{"id":97,"date":"2017-07-06T00:27:48","date_gmt":"2017-07-06T00:27:48","guid":{"rendered":"https:\/\/xiaohaionline.com\/?p=97"},"modified":"2017-07-06T20:46:15","modified_gmt":"2017-07-06T20:46:15","slug":"using-dropwizard-in-building-micro-services-in-java","status":"publish","type":"post","link":"https:\/\/xiaohaionline.com\/index.php\/2017\/07\/06\/using-dropwizard-in-building-micro-services-in-java\/","title":{"rendered":"Using dropwizard in building micro-services in Java"},"content":{"rendered":"<p><strong>Micro-services<\/strong> are the norm nowadays when building backend internet applications or business applications. Of course we would not want to build everything from scratch. What micro-service framework should we use then? If you work in Java, your choices basically boil down to either <a href=\"http:\/\/www.dropwizard.io\/\">Dropwizard<\/a> or <a href=\"https:\/\/spring.io\">Spring<\/a>. If you work in Scala, you have more choices like Twitter&#8217;s Finagle, Play framework and others.<\/p>\n<p>\nBetween <strong>Dropwizard<\/strong> and Spring, I personally prefer <strong>Dropwizard<\/strong>. For Dropwizard seems simpler and has a more elegant design. Then, what will Dropwizard do for us as a developer? Let&#8217;s quickly run through a micro-service building virtual exercise.\n<\/p>\n<p><!--more--><\/p>\n<p>\nObviously our micro-service will be built on top of a HTTP server. So we will need an embedded HTTP server as a starting point. When it comes to embedded HTTP servers, we had quite some options in the past. You can read <a href=\"https:\/\/java-source.net\/open-source\/web-servers\">here<\/a> for all the possible options. But, as of now, the clear winner is <a href=\"http:\/\/www.eclipse.org\/jetty\/\"><strong>Jetty<\/strong><\/a>. So we will use <a href=\"http:\/\/www.eclipse.org\/jetty\/\"><strong>Jetty<\/strong><\/a> as our embedded HTTP server.\n<\/p>\n<p>\nOur micro-service will need to support RESTful API. There is even a standard for that. It is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_API_for_RESTful_Web_Services\">JAX-RS<\/a>, Java API for RESTful Web Services (JAX-RS). The best implementation in this case is <a href=\"https:\/\/github.com\/jersey\"><strong>Jersey<\/strong><\/a>.<\/p>\n<p>With <a href=\"https:\/\/github.com\/jersey\"><strong>Jersey<\/strong><\/a>, you can use many pleasant markings to turn your POJOs (Plain Old Java Object) into web requests or web responses with easy. Take a peak at these markings:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"text codecolorer\">@Path, @GET, @PUT, @POST, @DELETE, @HEAD, @PathParam, @QueryParam, @MatrixParam, @HeaderParam, @CookieParam, @FormParam, @DefaultValue, @Context<\/div><\/div>\n<\/p>\n<p>Great, the <a href=\"https:\/\/github.com\/jersey\"><strong>Jersey<\/strong><\/a> does the heavy lifting of marshaling between web request\/response protocols and our Java objects so that we can focus on our Java models instead of HTTP protocols. There are still issues: we would like to load our application configurations from likely a jason\/yaml file, additionally we might want our request\/response to contain jsons. We will need another library. Yes, that library would be <a href=\"https:\/\/github.com\/FasterXML\/jackson\"><strong>Jackson<\/strong><\/a>.<\/p>\n<p>Apart from the above, we might also needs supports like <a href=\"http:\/\/metrics.dropwizard.io\/\"><strong>Metrics<\/strong><\/a>, <a href=\"http:\/\/logback.qos.ch\/\"><strong>Logging<\/strong><\/a>, <a href=\"http:\/\/www.jdbi.org\/\"><strong>JDBI<\/strong><\/a> and <a href=\"http:\/\/www.hibernate.org\/subprojects\/validator.html\">user input <strong>validator<\/strong><\/a> etc.<\/p>\n<p>The good news is, <a href=\"http:\/\/www.dropwizard.io\/\">Dropwizard<\/a> provides all of the things above. I hope by now, you are convinced that using <a href=\"http:\/\/www.dropwizard.io\/\">Dropwizard<\/a> is a good choice for you when building your micro services. You can simply think of <a href=\"http:\/\/www.dropwizard.io\/\">Dropwizard<\/a> as a bundle of libraries that reflects the best engineering practices in the Java world.<\/p>\n<p>To understand how a dropwizard project looks like and how they work, I found <a href=\"http:\/\/www.dropwizard.io\/1.1.2\/docs\/getting-started.html\">this<\/a> was a very good starting point.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Micro-services are the norm nowadays when building backend internet applications or business applications. Of course we would not want to build everything from scratch. What micro-service framework should we use then? If you work in Java, your choices basically boil down to either Dropwizard or Spring. If you work in Scala, you have more choices [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,3],"tags":[17,20,14,19,18,21],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-java","category-software","tag-dropwizard","tag-jackson","tag-java","tag-jersey","tag-jetty","tag-microservice"],"_links":{"self":[{"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":9,"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xiaohaionline.com\/index.php\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}