📜 ⬆️ ⬇️

We write REST API on Vert.x. Part 1: Setting Up the Environment

Hi, Harbouser. In this series of articles, we will write the REST API using Vert.x. Let's start with a simple one: install the vert.x itself and configure the launch of a simple application in the IDE.


Some general information about the framework

Vert.x is a kind of node.js from the world of Java. It is distinguished by its large list of supported languages ​​(at the time of writing: Java, JavaScript, CoffeeScript, Groovy, Python, Ruby, Clojure, Scala), as well as the presence of a mechanism for launching blocking operations and the ability to use multiple threads, while maintaining the principle of one workflow.

More information about the framework:
vertx.io/manual.html - Official Manual
habrahabr.ru/post/181686 - The only post I found on Habré about the framework
')
Installing the framework, setting up the IDE and running the sample project

First of all, let's install vert.x itself, for this we will go to the downloads section on the framework's official website . Unpack the archive, add the bin directory to PATH. We check the installation by running the following command in the terminal:

vertx version

And that's all. One of the main principles of the framework is simplicity of configuration.

Next, using apache maven, we will generate a sample project. To do this, run the following command in the terminal:

mvn archetype:generate -Dfilter=io.vertx:

Select among the archetype (at the time of this writing, no more than one of them) io.vertx: vertx-maven-archetype and select the latest version. We set the following parameters of the future application in the terminal: groupId, artifactId, version and basic package. For the example project, I set them accordingly: com.example, vertx-example, 1.0.0-SNAPSHOT, com.example. Check that the project is successfully assembled, for this we will execute the following command in the terminal:

mvn install

If you see the message BUILD SUCCESS, then everything is done correctly, and the project structure should look like this:



Set up the launch of the project. I configured in IntelliJ IDEA, but I assure you with other IDE problems should not be.

There is a small problem with resolving dependencies from the vertx-core, vertx-platform and vertx-hazelcast packages. In our project, now they have scope set for the provided, which means that they will be provided by the environment. However, running our application in an IDE that does not provide these packages, so we will create two maven profiles: one to run in the IDE, the other to build an application that will run directly on vert.x, which carries the necessary dependencies. To do this, add the following text to our pom.xml:
     <profiles>
         <profile>
             <id> local </ id>
             <properties>
                 <vertx-scope> compile </ vertx-scope>
             </ properties>
         </ profile>
         <profile>
             <id> deployment </ id>
             <properties>
                 <vertx-scope> provided </ vertx-scope>
             </ properties>
         </ profile>
     </ profiles>


And change the scope for the vertx packages:

         <dependency>
             <groupId> io.vertx </ groupId>
             <artifactId> vertx-core </ artifactId>
             <version> $ {vertx.version} </ version>
             <scope> $ {vertx-scope} </ scope>
         </ dependency>
         <dependency>
             <groupId> io.vertx </ groupId>
             <artifactId> vertx-platform </ artifactId>
             <version> $ {vertx.version} </ version>
             <scope> $ {vertx-scope} </ scope>
         </ dependency>
         <dependency>
             <groupId> io.vertx </ groupId>
             <artifactId> vertx-hazelcast </ artifactId>
             <version> $ {vertx.version} </ version>
             <scope> $ {vertx-scope} </ scope>
         </ dependency>


In order to launch a project with one click we will create the following launch configuration:



In essence, we run the program with org.vertx.java.platform.impl.cli.Starter as an input point and with the arguments runmod com.example~vertx-example~1.0-SNAPSHOT . The argument indicates which module to run. The naming logic is as follows: groupId ~ artifactId ~ version.

Now you can run our application, if at launch the output to the console looks like the following, then everything is done correctly:

 Apr 08, 2014 5:47:39 PM org.vertx.java.core.logging.impl.JULLogDelegate info
 INFO: PingVerticle started
 Apr 08, 2014 5:47:39 PM org.vertx.java.core.logging.impl.JULLogDelegate info
 INFO: Succeeded in deploying module


Thank you for attention. Next time, we’ll see what’s what in this project and start writing our own application.

The source code of the resulting project is available on GitHub: github.com/X3H4/vertx-example

Source: https://habr.com/ru/post/218733/


All Articles