Getting started with vert.x in java

Exposing web services using vert.x . For the beginners, follow steps and get started with the vert.x

  1. we need the below dependencies and it should be put in pom.xml file.
<dependencies>
<! —  https://mvnrepository.com/artifact/io.vertx/vertx-core
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.4.2</version>
</dependency>
<! —  https://mvnrepository.com/artifact/io.vertx/vertx-web
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>`

2. Create your class which extends AbstractVerticle

public class SimpleVertx extends AbstractVerticle {
@Override
public void start(Future<Void> fut) throws Exception {
Vertx vertx = Vertx.factory.vertx();
Router router = Router.router(vertx);
router.post(“/user”).handler(this::createUser);
router.get(“/user”).handler(this::getUser);
vertx.createHttpServer().requestHandler(router::accept).listen(config().getInteger(“http.port”, 8080),
result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(“it failed”);
}
});
}
private void getUser(RoutingContext routingContext) {
User user = new User();
user.setGender(“Male”);
user.setName(“shashi raj”);
HttpServerResponse res = routingContext.response();
res.setStatusCode(200).putHeader(“content-type”, “application/json”).end(user.toString());
}
private void createUser(RoutingContext routingContext){
ObjectMapper mapper=new ObjectMapper();
User user=mapper.convertValue(routingContext.getBodyAsJson(), User.class);
//dao.createUser(user);
HttpServerResponse res = routingContext.response();
res.setStatusCode(201).putHeader(“content-type”, “application/json”).end();
}
}

3. Now we need the below build module in pom to create FAT jar

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</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>com.raj.shashi.vertx.examples.SimpleVertx</Main-Class>
<Build-Number>1.0</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

4. we need to make sure that we have installed vert.x on our operating system . since I am using mac . so, mac users it can be download using following below steps

Install the App
Press Command+Space and type Terminal and press enter/return key.
Run in Terminal app:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
and press enter/return key. Wait for the command to finish.
Run:
brew install vert.x
Done! You can now use vert.x.

5. In order to run it from the command line , go to the directory and run below command

vertx run SimpleVertx.java

6. Now we are done . just go and hit the same url in postman. And yes , we have started .

http://localhost:8080/user GET
http://localhost:8080/user POST 
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.