ããã¯ããªã«ãããããŠæžãããã®ïŒ
ãããããVert.xãå°ãç¥ã£ãŠãããæ¹ãããã®ããªãŒãšæããŸããŠã
詊ããŠã¿ããããªãšã
Vert.xãšã¯
Vert.xã¯ãã€ãã³ãé§åããã³ããããã³ã°ãè€æ°ã®èšèªã«ããéçºãå¯èœãªãã¬ãŒã ã¯ãŒã¯ã§ãã
è€æ°ã®èšèªãšã¯ãJavaãKotlinãJavaScriptãGroovyãRubyãScalaãæããŸãã
ãŸã è¥ããã¬ãŒã ã¯ãŒã¯ããšãããšããã§ããªãããã£ããåããååšããŠããŸãã
çŸåšã®ããŒãžã§ã³ã¯3.9ç³»ã§ããã4ãéçºäžã§ãã
4ã§ã¯JavaãKotlinãGroovyããããã¿ãŒã²ããã«ãªããã§ãããããïŒïŒã³ãŒãäŸãæžã£ãŠãïŒ
çŸåšã®ããŒãžã§ã³ã®ããã¥ã¡ã³ãã¯ããã¡ãã§ãã
ãšãããããå§ããŠã¿ãŸããããã
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã§ãã
$ java --version openjdk 11.0.9.1 2020-11-04 OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing) $ mvn --version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 11.0.9.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: ja_JP, platform encoding: UTF-8 OS name: "linux", version: "5.4.0-53-generic", arch: "amd64", family: "unix"
å§ããŠã¿ã
Vert.xã«ã¯ãStarterãšåŒã°ããåæãããžã§ã¯ããäœãWebãµã€ãããããŸãã
Vert.x Starter - Create new Eclipse Vert.x applications
ãµã³ãã«ã¯ãã¡ãã®ãªããžããªã«ãããŸãã
https://github.com/vert-x3/vertx-examples/tree/master
Webã¢ããªã±ãŒã·ã§ã³ã®ãµã³ãã«ã¯ããã¡ãã
https://github.com/vert-x3/vertx-examples/tree/master/web-examples
ã§ãã©ãããŸããããšãããšããã§ãããä»åã¯Starterãããããžã§ã¯ããäœã£ãŠå§ããŠã¿ãããšæããŸãã
ãŸãã¯ããããžã§ã¯ãçšã®ãã£ã¬ã¯ããªãäœæã
$ mkdir hello-web $ cd hello-web
Starterã§ãããžã§ã¯ãã.tar.gzãã¡ã€ã«ã§äœæããŠãããŠã³ããŒãããŸãã
$ curl -s -G https://start.vertx.io/starter.tar.gz \ -d "groupId=org.littlewings" \ -d "artifactId=hello-web" \ -d "packageName=org.littlewings.vertx.web" \ -d "vertxVersion=3.9.4" \ -d "vertxDependencies=vertx-web" \ -d "language=java" \ -d "jdkVersion=11" \ -d "buildTool=maven" \ -o - | \ tar -zxvf -
Starterã®ãµã€ãã«ã¯curlã®äœ¿çšäŸãèŒã£ãŠããŠããã¡ãã¯.zipã§æžãããŠããŸãããæ¡åŒµåã倿Žããããšã§.tar.gzã«
ããããšãã§ããŸãã
äŸåé¢ä¿ã«ã¯vertx-webãå ããVert.xã®ããŒãžã§ã³ã¯3.9.4ã§äœæã
ã§ãããã£ããããžã§ã¯ãã«ã¯ããããªæãã®ãã¡ã€ã«ãå«ãŸããŠããŸãã
$ find -type f ./.editorconfig ./.mvn/wrapper/MavenWrapperDownloader.java ./.mvn/wrapper/maven-wrapper.jar ./.mvn/wrapper/maven-wrapper.properties ./pom.xml ./README.adoc ./.gitignore ./mvnw ./mvnw.cmd ./src/main/java/org/littlewings/vertx/web/MainVerticle.java ./src/test/java/org/littlewings/vertx/web/TestMainVerticle.java
å°ãã代衚çãªãã¡ã€ã«ãèŠãŠã¿ãŸãããã
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.littlewings</groupId> <artifactId>hello-web</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version> <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> <exec-maven-plugin.version>1.5.0</exec-maven-plugin.version> <vertx.version>3.9.4</vertx.version> <junit-jupiter.version>5.4.0</junit-jupiter.version> <main.verticle>org.littlewings.vertx.web.MainVerticle</main.verticle> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-stack-depchain</artifactId> <version>${vertx.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-junit5</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <release>11</release> </configuration> </plugin> <plugin> <artifactId>maven-shade-plugin</artifactId> <version>${maven-shade-plugin.version}</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>io.vertx.core.Launcher</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource> </transformer> </transformers> <artifactSet> </artifactSet> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar </outputFile> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>${exec-maven-plugin.version}</version> <configuration> <mainClass>io.vertx.core.Launcher</mainClass> <arguments> <argument>run</argument> <argument>${main.verticle}</argument> </arguments> </configuration> </plugin> </plugins> </build> </project>
Vert.xã«é¢ããããŒãžã§ã³å šäœã¯BOMã§æå®ãã
<dependencyManagement> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-stack-depchain</artifactId> <version>${vertx.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
äŸåé¢ä¿ã«ã¯ãæå®ããvertx-webãå«ãŸããŠããŸãã
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> </dependency>
Maven Shade Pluginã§ãåäžã®JARãäœããããã«ãæ§æãããŠããŸãã
<plugin> <artifactId>maven-shade-plugin</artifactId> <version>${maven-shade-plugin.version}</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>io.vertx.core.Launcher</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource> </transformer> </transformers> <artifactSet> </artifactSet> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar </outputFile> </configuration> </execution> </executions> </plugin>
èµ·åã¯ã©ã¹ã¯Vert.xã®ãã®ã䜿ã
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>io.vertx.core.Launcher</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer>
ã§ããããJARãã¡ã€ã«ã¯ã-fat.jarãšããååã«ãªããŸãããšã
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar </outputFile>
src/main/javaã«å«ãŸããŠããã¯ã©ã¹ã¯ããã¡ãã
src/main/java/org/littlewings/vertx/web/MainVerticle.java
package org.littlewings.vertx.web; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; public class MainVerticle extends AbstractVerticle { @Override public void start(Promise<Void> startPromise) throws Exception { vertx.createHttpServer().requestHandler(req -> { req.response() .putHeader("content-type", "text/plain") .end("Hello from Vert.x!"); }).listen(8888, http -> { if (http.succeeded()) { startPromise.complete(); System.out.println("HTTP server started on port 8888"); } else { startPromise.fail(http.cause()); } }); } }
Verticleã£ãŠãªãã§ãããïŒ
Vert.xã®Coreã®ããã¥ã¡ã³ãã«æžãããŠãããã§ãã
Vert.x Coreã®ããã¥ã¡ã³ããèªã¿ã€ã€ãäœæãããããžã§ã¯ããåãããŠã¿ã
Vert.x Coreã®ããã¥ã¡ã³ãããå°ãçºããŠã¿ãŸãã
Vert.x Coreã¯ã次ã®ãããªæ©èœãæäŸããŸãã
- Writing TCP clients and servers
- Writing HTTP clients and servers including support for WebSockets
- The Event bus
- Shared data - local maps and clustered distributed maps
- Periodic and delayed actions
- Deploying and undeploying Verticles
- Datagram Sockets
- DNS client
- File system access
- High availability
- Native transports
- Clustering
Vert.x Coreã®æ©èœã¯Low Levelãªãã®ãšãããŠããŠãããŒã¿ããŒã¹ã¢ã¯ã»ã¹ãèªèšŒãHigh Levelã®Webæ©èœãªã©ã¯å«ãŸããŸããã
ãããŠãå°ãã軜éã§ãããšã
Vert.x Coreã¯ãä»åäœæãããããžã§ã¯ãã®å ŽåãVert.x Webããã®æšç§»çäŸåé¢ä¿ã«å«ãŸããŸãã
mvn dependency:treeã®çµæãæç²ã
[INFO] +- io.vertx:vertx-web:jar:3.9.4:compile [INFO] | +- io.vertx:vertx-web-common:jar:3.9.4:compile [INFO] | +- io.vertx:vertx-auth-common:jar:3.9.4:compile [INFO] | +- io.vertx:vertx-bridge-common:jar:3.9.4:compile [INFO] | \- io.vertx:vertx-core:jar:3.9.4:compile [INFO] | +- io.netty:netty-common:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-buffer:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-transport:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-handler:jar:4.1.49.Final:compile [INFO] | | \- io.netty:netty-codec:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-handler-proxy:jar:4.1.49.Final:compile [INFO] | | \- io.netty:netty-codec-socks:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-codec-http:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-codec-http2:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-resolver:jar:4.1.49.Final:compile [INFO] | +- io.netty:netty-resolver-dns:jar:4.1.49.Final:compile [INFO] | | \- io.netty:netty-codec-dns:jar:4.1.49.Final:compile [INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.11.3:compile [INFO] | \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.3:compile [INFO] | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.3:compile
Vert.x Coreã®ããã¥ã¡ã³ããããããã€ãç¹åŸŽã泚æäºé ãæãåºããŸãã
ã€ãã³ãé§ååãªã®ã§ãVert.xåŽãã¢ããªã±ãŒã·ã§ã³ãåŒã³åºããŸãã
Donât call us, weâll call you.
IOãªã©ã§ããããã¯ããŠã¯ãããŸããã
Vert.xã¯ãã€ãã³ãé§ååã§ã¯ãããŸããã·ã³ã°ã«ã¹ã¬ããã§ã¯ãªããè€æ°ã®ã€ãã³ãã«ãŒããæã¡ãŸãã
ã€ãã³ãã«ãŒãããããã¯ããªãããšã
The Golden Rule - Donât Block the Event Loop
JDBCãªã©ããããã¯ããAPIã䜿ãå Žåã¯å°çšã®APIïŒå¥ã®ã¹ã¬ããããŒã«ïŒãä»ããŠåŒã³åºãããšã
è€æ°ã®éåæåŠççµæã調æŽããå Žåã
ãšãŸããã€ãã³ãé§åãã€ãã³ããããã³ã°ãªèãã§ã¢ããªã±ãŒã·ã§ã³ãäœãããã®ãã¬ãŒã ã¯ãŒã¯ã§ããããšã
ãããããŠããããŸãã
ã§ãèªã¿é²ããŠãããšVerticlesãåºãŠããŸãã
ã©ãããå¿ ã䜿ããã®ã§ã¯ãªãããã§ãã
This model is entirely optional and Vert.x does not force you to create your applications in this way if you donât want to..
Verticleã¯ãVert.xã«ãã£ãŠãããã€ããã³å®è¡ããããã³ãŒãã®ãã£ã³ã¯ã§ããActorã¢ãã«ã«ããããActorã«äŒŒããã®ãš
èãããšãããããªã
Vert.xã€ã³ã¹ã¿ã³ã¹ã¯ããã©ã«ãã§CPUã³ã¢ à 2ã®ã€ãã³ãã«ãŒãã¹ã¬ãããä¿æãããã®äžã§è€æ°ã®Verticleã€ã³ã¹ã¿ã³ã¹ã
å«ãŸããããã«æ§æãããã®ã ãšãããŸããç°ãªãVerticleã€ã³ã¹ã¿ã³ã¹ã¯Event Busã䜿ã£ãã¡ãã»ãŒãžéä¿¡ãå¯èœã§ãããšã
èŠããã«ãVert.xã«ããããããã€ã®åäœã®ããã«èŠããŸãã
ãšãããããä»ããVerticleãåãããŠã¿ãŸããããã
äœæãããããžã§ã¯ãããã«ãããŠã¿ãŸãã
$ mvn package -DskipTests=true
Fat JARãèµ·åã
$ java -jar target/hello-web-1.0.0-SNAPSHOT-fat.jar HTTP server started on port 8888 11æ 15, 2020 9:35:13 ååŸ io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer æ å ±: Succeeded in deploying verticle
ããèŠããšãVerticleããããã€ãããã£ãŠæžããŠãŸããã
確èªã
$ curl -i localhost:8888 HTTP/1.1 200 OK content-type: text/plain content-length: 18 Hello from Vert.x!
ã¡ãã»ãŒãžãè¿ã£ãŠããŸãããHello fromãã¯ã©ãã§åºåããŠããã®ã§ãããïŒ
ãšããããã§ãå ã»ã©ã®ããã°ã©ã ãèŠè¿ããŸãïŒæç²ïŒã
vertx.createHttpServer().requestHandler(req -> {
req.response()
.putHeader("content-type", "text/plain")
.end("Hello from Vert.x!");
}).listen(8888, http -> {
if (http.succeeded()) {
startPromise.complete();
System.out.println("HTTP server started on port 8888");
} else {
startPromise.fail(http.cause());
}
});
ãã£ããå ¥ã£ãŠããŸããã
ãšããã§ããã®ç¶æ ã ãšãªã¯ãšã¹ãã®ãã¹ã«é¢ããããã€ãåãçµæãè¿ããŸãã
$ curl -i localhost:8888/foo/bar HTTP/1.1 200 OK content-type: text/plain content-length: 18 Hello from Vert.x!
ã©ããã£ãŠèµ·åããŠããããä»äžåºŠèŠè¿ããŠã¿ãŸãããã
Maven Shade Pluginã§ã¯ããããªèšå®ãå ¥ã£ãŠãããã§ããã
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>io.vertx.core.Launcher</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer>
ãã®ããããå°ã远ã£ãŠã¿ãŸãã
https://github.com/eclipse-vertx/vert.x/blob/3.9.4/src/main/java/io/vertx/core/Launcher.java
ãããã®ã¯ã©ã¹å ã§ãæå®ãããVerticleïŒä»åã¯èªåçæããããã®ïŒããããã€ããŠããããã§ãã
ãŸããããã©ã«ãã§runãšããã³ãã³ããå®è¡ããŠãããããªã®ã§ãFat JARã®ãã«ããèŠãŠã¿ãŸãã
$ java -jar target/hello-web-1.0.0-SNAPSHOT-fat.jar --help
Usage: java -jar target/hello-web-1.0.0-SNAPSHOT-fat.jar [COMMAND] [OPTIONS]
[arg...]
Commands:
bare Creates a bare instance of vert.x.
list List vert.x applications
run Runs a verticle called <main-verticle> in its own instance of
vert.x.
start Start a vert.x application in background
stop Stop a vert.x application
version Displays the version.
Run 'java -jar target/hello-web-1.0.0-SNAPSHOT-fat.jar COMMAND --help' for more
information on a command.
ä»ã«ãããã€ãã³ãã³ãããããŸãããã³ãã³ãã®å®è£ ã¯ã以äžã«ãããŸãã
ãŸãããªããšãªãããã£ãŠãããããªâŠïŒ
詊ãã«ãVerticleã䜿ããã«ãä»ã®Verticleã«è¿ãåŠçãæžããŠã¿ãŸãããããããªæãã§ããããã
src/main/java/org/littlewings/vertx/web/App.java
package org.littlewings.vertx.web; import io.vertx.core.Vertx; import io.vertx.core.http.HttpServer; public class App { public static void main(String... args) { Vertx vertx = Vertx.vertx(); HttpServer server = vertx.createHttpServer(); server.requestHandler(req -> req .response() .putHeader("content-type", "text/plain") .end("Hello from Vert.x!") ); server.listen(8888); System.out.println("HTTP server started on port 8888"); } }
ãããçŽæ¥èµ·åããŠãcurlã§ç¢ºèªã
$ curl -i localhost:8888 HTTP/1.1 200 OK content-type: text/plain content-length: 18 Hello from Vert.x!
OKã§ãã
ãªããmvn exec:javaã§å®è¡ããããšãããšãçæãããpom.xmlã¯Verticleãå®è¡ããããã«æ§æãããŠããã®ã§
ãã®èšå®ã®ãŸãŸ-Dexec.mainClassãæå®ããŠãããŸããããŸããã
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>${exec-maven-plugin.version}</version> <configuration> <mainClass>io.vertx.core.Launcher</mainClass> <arguments> <argument>run</argument> <argument>${main.verticle}</argument> </arguments> </configuration> </plugin>
ãæ³šæãã
Vert.x WebãèŠã
çŸæç¹ã§å°ã䜿ã£ãŠããŸãããVert.x Webãããå°ã䜿ã£ãŠã¿ãŸãã
ã³ã³ã»ãããã«ãŒãã£ã³ã°ã«é¢ããããã¥ã¡ã³ãã
ãªã¯ãšã¹ããåŠçããHandlerãããããã§ã€ã³ãå¯èœã
Handling requests and calling the next handler
If you donât end the response in your handler, you should call next so another matching route can handle the request (if any).
ã«ãŒãã£ã³ã°ã¯åºå®ã®ãã¹ãåæ¹äžèŽãæ£èŠè¡šçŸãHTTPã¡ãœãããæå®ã§ãããã¹ã«ãã©ã¡ãŒã¿ãŒãå«ããŠãã£ããã£ã
ã§ããŸãã
Routing by paths that begin with something
Routing with regular expressions
HTTPããã£ãæ±ãå Žåã
ã§ã¯ããã®ããããèŠã€ã€ãèªåçæãããVerticleãã«ã¹ã¿ãã€ãºããŠãããšããŸãããã
package org.littlewings.vertx.web; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerResponse; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.BodyHandler; public class MainVerticle extends AbstractVerticle { @Override public void start(Promise<Void> startPromise) throws Exception { // ããã«åŠçãæžã } }
ãŸãã¯ãHttpServerã®äœæãšRouterã®ååŸã
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
// ããã«Handlerã®å®è£
ãæžã
server.requestHandler(router);
server.listen(8888, http -> {
if (http.succeeded()) {
startPromise.complete();
System.out.println("HTTP server started on port 8888");
} else {
startPromise.fail(http.cause());
}
});
ãããŸã§ããåºå®ã®å®è£ ãšããŸãã
以éã¯ãã³ã¡ã³ãã®éšåã®å®è£
ã倿Žãã€ã€ã以äžã®ã³ãã³ãã§JARã®åäœæããã³ããã°ã©ã ã®èµ·åãçŽããç¹°ãè¿ããŠãããš
æã£ãŠèªãã§ãã ããã
$ mvn package -DskipTests=true && java -jar target/hello-web-1.0.0-SNAPSHOT-fat.jar
ãŸãã¯Routerã«ç¹ã«ãªã«ãèšå®ããHandlerã远å ã
router.route().handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response
.putHeader("content-type", "text/plain")
.end("Hello from Vert.x!");
});
èªåçæãããåŠçã§ã䌌ããããªHandlerããããã®ãèšå®ãããŠããŸããããä»åã¯RoutingContextãæž¡ãããŸãã
èªåçæãããã³ãŒãã§ã¯ãHttpServerRequestãæž¡ãããŠããŸããããªããããã¯RoutingContextãããååŸã§ããŸãã
確èªã
$ curl -i localhost:8888 HTTP/1.1 200 OK content-type: text/plain content-length: 18 Hello from Vert.x!
次ã«ãHandlerãè€æ°ãã§ã€ã³ãããŠã¿ãŸãããã1床RouteãååŸããŠãããã«Handlerãè¶³ããŠãããŸãã
Route route = router.route();
route.handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "text/plain");
routingContext.next();
});
route.handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.end("Hello from Vert.x!");
});
æåã®Handlerã¯RoutingContext#nextãåŒã³åºããŠãããšããããã€ã³ãã§ãæåŸãšãªãHandlerã¯HttpServerResponse#endã
åŒã³åºãå¿
èŠããããŸãã
å ã»ã©ãšåäœã¯å€ãããªãã®ã§ã確èªã¯çç¥ã
次ã¯ãäžæ°ã«ãã¹ã®æå®ãQueryStringã®ååŸãHTTPããã£ãæ±ãããã«ããŠã¿ãŸãããã
Route getRoute = router.route("/echo").method(HttpMethod.GET); getRoute.handler(routingContext -> { HttpServerRequest request = routingContext.request(); HttpServerResponse response = routingContext.response(); String message = request.getParam("message"); response.end(String.format("Server reply, [%s]", message)); }); Route postRoute = router.route("/json").method(HttpMethod.POST); postRoute.handler(BodyHandler.create()); postRoute.handler(routingContext -> { String bodyAsString = routingContext.getBodyAsString(); System.out.println(bodyAsString); JsonObject json = routingContext.getBodyAsJson(); String message = json.getString("message"); HttpServerResponse response = routingContext.response(); response.end(String.format("Server reply, [%s]", message)); });
RouteãååŸããéã«ãã¹ãæå®ãããŸãHTTPã¡ãœãããæå®ããŸãã
Route getRoute = router.route("/echo").method(HttpMethod.GET);
QueryStringã¯ãHttpServerRequest#getParamã§ãã©ã¡ãŒã¿ãŒãšããŠæ±ããããã§ãã
String message = request.getParam("message");
HTTPããã£ãæ±ãå Žåã¯ã1床BodyHandlerã远å ããŠ
postRoute.handler(BodyHandler.create());
ãã®åŸã«ãHandlerããã§ã€ã³ãããŸãã
postRoute.handler(routingContext -> {
String bodyAsString = routingContext.getBodyAsString();
System.out.println(bodyAsString);
JsonObject json = routingContext.getBodyAsJson();
String message = json.getString("message");
HttpServerResponse response = routingContext.response();
response.end(String.format("Server reply, [%s]", message));
});
ä»åã¯ãHTTPããã£ãæååãšããŠååŸããŠæšæºåºåã«æžãåºãã€ã€ãJSONãšããŠãæ±ã£ãŠããŸãã
確èªã
GETã§QueryStringã
$ curl -i 'localhost:8888/echo?message=Hello%20World!!' HTTP/1.1 200 OK content-length: 29 Server reply, [Hello World!!]
POSTã§JSONéä¿¡ã
$ curl -i localhost:8888/json -d '{"message": "Hello World!!"}'
HTTP/1.1 200 OK
content-length: 29
Server reply, [Hello World!!]
POSTã®æã¯ããµãŒããŒåŽã«ãããªæãã§HTTPããã£ã®å 容ãåºåãããŸãã
{"message": "Hello World!!"}
OKã§ããã
ãŸãšã
ä»åã¯ãVert.xã®ããããåºæ¬çãªéšåãšãVert.x Webãå°ãæ±ã£ãŠã¿ãŸããã
ããé°å²æ°ãããã£ãŠããæããããã®ã§ãããããã¡ãã£ãšãã€æ £ããŠãã£ãŠã¿ãŸãããã
æåŸã«ãä»åããããè§Šã£ãVerticleã®å
šäœãèŒããŠãããŸãã
src/main/java/org/littlewings/vertx/web/MainVerticle.java
package org.littlewings.vertx.web; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerResponse; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.BodyHandler; public class MainVerticle extends AbstractVerticle { @Override public void start(Promise<Void> startPromise) throws Exception { HttpServer server = vertx.createHttpServer(); Router router = Router.router(vertx); /* router.route().handler(routingContext -> { HttpServerResponse response = routingContext.response(); response .putHeader("content-type", "text/plain") .end("Hello from Vert.x!"); }); */ /* Route route = router.route(); route.handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "text/plain"); routingContext.next(); }); route.handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.end("Hello from Vert.x!"); }); */ Route getRoute = router.route("/echo").method(HttpMethod.GET); getRoute.handler(routingContext -> { HttpServerRequest request = routingContext.request(); HttpServerResponse response = routingContext.response(); String message = request.getParam("message"); response.end(String.format("Server reply, [%s]", message)); }); Route postRoute = router.route("/json").method(HttpMethod.POST); postRoute.handler(BodyHandler.create()); postRoute.handler(routingContext -> { String bodyAsString = routingContext.getBodyAsString(); System.out.println(bodyAsString); JsonObject json = routingContext.getBodyAsJson(); String message = json.getString("message"); HttpServerResponse response = routingContext.response(); response.end(String.format("Server reply, [%s]", message)); }); server.requestHandler(router); server.listen(8888, http -> { if (http.succeeded()) { startPromise.complete(); System.out.println("HTTP server started on port 8888"); } else { startPromise.fail(http.cause()); } }); } }