{"id":6020,"date":"2018-03-22T21:03:34","date_gmt":"2018-03-22T21:03:34","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6020"},"modified":"2018-03-22T21:03:34","modified_gmt":"2018-03-22T21:03:34","slug":"first-look-at-eclipse-vert-x","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/first-look-at-eclipse-vert-x\/","title":{"rendered":"First look at Eclipse Vert.x"},"content":{"rendered":"<p>Eclipse Vert.x is a lightweight framework for developing web applications, microservices and more.<\/p>\n<p>I&#8217;ve created a project using IntelliJ named VertxTest based upon a Maven project to try out the routing functionality in Vert.x.<\/p>\n<p><strong>Dependencies &#8211; the pom<\/strong><\/p>\n<p>Our pom.xml looks like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot;\r\n         xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n         xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;\r\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\r\n    &lt;groupId&gt;VertxTest&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;VertxTest&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\r\n\r\n    &lt;properties&gt;\r\n        &lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n        &lt;vertx.version&gt;3.5.0&lt;\/vertx.version&gt;\r\n    &lt;\/properties&gt;\r\n    &lt;build&gt;\r\n        &lt;plugins&gt;\r\n            &lt;plugin&gt;\r\n                &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n                &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n                &lt;version&gt;3.5.1&lt;\/version&gt;\r\n                &lt;configuration&gt;\r\n                    &lt;source&gt;${java.version}&lt;\/source&gt;\r\n                    &lt;target&gt;${java.version}&lt;\/target&gt;\r\n                &lt;\/configuration&gt;\r\n            &lt;\/plugin&gt;\r\n            &lt;plugin&gt;\r\n                &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n                &lt;artifactId&gt;maven-shade-plugin&lt;\/artifactId&gt;\r\n                &lt;version&gt;2.4.3&lt;\/version&gt;\r\n                &lt;executions&gt;\r\n                    &lt;execution&gt;\r\n                        &lt;phase&gt;package&lt;\/phase&gt;\r\n                        &lt;goals&gt;\r\n                            &lt;goal&gt;shade&lt;\/goal&gt;\r\n                        &lt;\/goals&gt;\r\n                        &lt;configuration&gt;\r\n                            &lt;transformers&gt;\r\n                                &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;\r\n                                    &lt;manifestEntries&gt;\r\n                                        &lt;Main-Class&gt;io.vertx.core.Launcher&lt;\/Main-Class&gt;\r\n                                        &lt;Main-Verticle&gt;com.putridparrot.MainVerticle&lt;\/Main-Verticle&gt;\r\n                                    &lt;\/manifestEntries&gt;\r\n                                &lt;\/transformer&gt;\r\n                                &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.AppendingTransformer&quot;&gt;\r\n                                    &lt;resource&gt;META-INF\/services\/io.vertx.core.spi.VerticleFactory&lt;\/resource&gt;\r\n                                &lt;\/transformer&gt;\r\n                            &lt;\/transformers&gt;\r\n                            &lt;artifactSet&gt;\r\n                            &lt;\/artifactSet&gt;\r\n                            &lt;outputFile&gt;${project.build.directory}\/${project.artifactId}-${project.version}-fat.jar&lt;\/outputFile&gt;\r\n                        &lt;\/configuration&gt;\r\n                    &lt;\/execution&gt;\r\n                &lt;\/executions&gt;\r\n            &lt;\/plugin&gt;\r\n            &lt;plugin&gt;\r\n                &lt;groupId&gt;org.codehaus.mojo&lt;\/groupId&gt;\r\n                &lt;artifactId&gt;exec-maven-plugin&lt;\/artifactId&gt;\r\n                &lt;version&gt;1.5.0&lt;\/version&gt;\r\n                &lt;configuration&gt;\r\n                    &lt;mainClass&gt;io.vertx.core.Launcher&lt;\/mainClass&gt;\r\n                    &lt;arguments&gt;\r\n                        &lt;argument&gt;run&lt;\/argument&gt;\r\n                        &lt;argument&gt;com.putridparrot.MainVerticle&lt;\/argument&gt;\r\n                    &lt;\/arguments&gt;\r\n                &lt;\/configuration&gt;\r\n            &lt;\/plugin&gt;\r\n        &lt;\/plugins&gt;\r\n    &lt;\/build&gt;\r\n    &lt;dependencies&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;io.vertx&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;vertx-core&lt;\/artifactId&gt;\r\n            &lt;version&gt;${vertx.version}&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;io.vertx&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;vertx-unit&lt;\/artifactId&gt;\r\n            &lt;version&gt;${vertx.version}&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;io.vertx&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;vertx-web&lt;\/artifactId&gt;\r\n            &lt;version&gt;${vertx.version}&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p><strong>The Verticle<\/strong><\/p>\n<p>As you can see from this my class MainVerticle (which extends AbstractVerticle) is created in the package com.putridparrot and the code looks like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot;\r\n\r\nimport io.vertx.core.AbstractVerticle;\r\nimport io.vertx.ext.web.Router;\r\nimport io.vertx.ext.web.handler.BodyHandler;\r\n\r\npublic class MainVerticle extends AbstractVerticle {\r\n    @Override\r\n    public void start() {\r\n\r\n        Router router = Router.router(vertx);\r\n\r\n        router.route(&quot;\/hello&quot;).handler(ctx -&gt; {\r\n            ctx.response()\r\n                .putHeader(&quot;content-type&quot;, &quot;text\/plain&quot;)\r\n                .end(&quot;Hello &quot; + ctx.queryParam(&quot;name&quot;));\r\n        });\r\n\r\n        vertx.createHttpServer()\r\n            .requestHandler(router::accept)\r\n            .listen(8080);\r\n\r\n        System.out.println(&quot;HTTP server started on port 8080&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><em>Note: queryParam actually returns a List<String> so in this example we would probably want to actually check the result and then get items from the list.<\/em><\/p>\n<p>We extend AbstractVerticle overriding the <em>start<\/em> method to handle the setting up of our routes etc. <\/p>\n<p><strong>Building and Running<\/strong><\/p>\n<p>We execute <em>mvn clean install<\/em> and then <em>mvn package<\/em> to generate the executable jar. Finally we can run this using <em>java -jar target\/VertxTest-1.0-SNAPSHOT-fat.jar<\/em> or similar in a configuration in an IDE (for example a JAR Application configuration within Intelli J just requires the path to the JAR listed above).<\/p>\n<p><strong>Testing and more<\/strong><\/p>\n<p>Now we should be able to run the following from your preferred web browser or Intelli J&#8217;s REST client (or similar in another IDE).<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:8080\/hello?name=Mark\r\n<\/pre>\n<p>We can easily add further routes and\/or handle GET or POST HTTP methods. For example here&#8217;s the addition of an error route using the <em>get<\/em> method<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nrouter.get(&quot;\/error&quot;)\r\n   .handler(this::handleGetError);\r\n<\/pre>\n<p>and<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate void handleGetError(RoutingContext ctx) {\r\n   ctx.response()\r\n      .setStatusCode(500)\r\n      .end();\r\n}\r\n<\/pre>\n<p>Now browsing to <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:8080\/error\r\n<\/pre>\n<p>will display a 500 error.<\/p>\n<p><strong>Static pages<\/strong><\/p>\n<p>We can also add static pages by declaring a route such as<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n router.route(&quot;\/pages\/*&quot;)\r\n   .handler((StaticHandler.create(&quot;pages&quot;)));\r\n<\/pre>\n<p>where the pages directory is located off the root off of the project folder (in this example). Simply place your HTML pages in this folder and you can access them using the following URL <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:8080\/pages\/index.html\r\n<\/pre>\n<p><strong>Routing based upon mime type<\/strong><\/p>\n<p>Along with routine based upon the specific query\/resource requested we can also route based upon the Content-Type within a request. For example if using HTTP POST with Content-Type application\/json we would want to return JSON, likewise if Content-Type is  application\/xml we&#8217;d expect XML return (and so on).<\/p>\n<p>To route based upon the mime type we simply write the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nrouter.route()\r\n   .consumes(&quot;*\/json&quot;)\r\n   .handler(ctx -&gt; {\r\n      System.out.println(&quot;JSON request&quot;);\r\n});\r\n\r\nrouter.route()\r\n   .consumes(&quot;*\/xml&quot;)\r\n   .handler(ctx -&gt; {\r\n      System.out.println(&quot;XML request&quot;);\r\n});\r\n<\/pre>\n<p>Obviously in the examples above we&#8217;re simply routing off of the root &#8220;\/&#8221; URL. Supply a path within the <em>route<\/em> method, as per previous example for other routing scenarios.<\/p>\n<p><strong>Running our Vertx application<\/strong><\/p>\n<p>We&#8217;ve seen we can create and run the JAR, but we can also create a Vertx application by creating a main method that looks something like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) {\r\n   Vertx vertx = Vertx.vertx();\r\n\r\n   vertx.deployVerticle(new MainVerticle());\r\n}\r\n<\/pre>\n<p>We use <em>deployVerticle<\/em> along with our Verticle and Vertx does the rest.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eclipse Vert.x is a lightweight framework for developing web applications, microservices and more. I&#8217;ve created a project using IntelliJ named VertxTest based upon a Maven project to try out the routing functionality in Vert.x. Dependencies &#8211; the pom Our pom.xml looks like this &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt; &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt; &lt;groupId&gt;VertxTest&lt;\/groupId&gt; &lt;artifactId&gt;VertxTest&lt;\/artifactId&gt; &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161,210],"tags":[],"class_list":["post-6020","post","type-post","status-publish","format-standard","hentry","category-java","category-vert-x"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6020"}],"version-history":[{"count":14,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6020\/revisions"}],"predecessor-version":[{"id":6035,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6020\/revisions\/6035"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}