Tuesday 17 May 2016

Rapid REST API Development With Dropwizard






by Sarang Nagmote



Category - Enterprise Integration
More Information & Updates Available at: http://vibranttechnologies.co.in


As a developer, you all know about REST APIs. There are tons of frameworks available in Java to develop RESTful web services. Jersey, RESTeasy, and Spring are some of the popular frameworks available in the market. Have you ever felt your REST API development process was set up in a straight forward way? Probably not!
But one framework which I evaluated recently has mostly positive aspects, and it allowed me to focus on my business problems instead of the plumbing work to prepare the infrastructure. This is none other than “Dropwizard
This tiny framework came very late in the market when compared to most of the other frameworks and attract the developer’s attention is a shorter time. In 2012 it was mentioned in the Thoughtworks technology radar as “TRIAL”. It jumped to “ADAPT” with-in 2 years gap (Refer https://www.thoughtworks.com/radar/languages-and-frameworks/dropwizard)

Why I like Dropwizard

  • Easy development setup
  • In-built metrics infrastructure
  • Health check
  • Embedded HTTP Server

Beauty Of Dropwizard

Dropwizard is like a developers BOX. It contains all the good parts which are required for easy development. For example, it contains the following well known APIs and frameworks to ease our development:
  • REST API – Jersey
  • Logging – Logback
  • Deployment – Jetty
  • Metrics – Metrics library
  • Health check – In built
  • Collection Util – Google Guava
  • JSON Parsing – Jackson library
  • and many more…
Ok we have seen some of the good parts of Dropwizard framework. In the next section we will quickly see the code for developing a REST API. First we have to create a Maven based project and add the following changes in your pom.xml
<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/maven-v4_0_0.xsd"> <version>1.0-SNAPSHOT</version> <name>dropwizardskol</name> <modelVersion>4.0.0</modelVersion> <groupId>com.cloudskol.dropwizardskol</groupId> <artifactId>dropwizardskol</artifactId> <packaging>jar</packaging> <url>http://maven.apache.org</url> <build> <finalName>${project.name}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <configuration> <createDependencyReducedPom>true</createDependencyReducedPom> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.cloudskol.dropwizardskol.DropwizardSkolApplication</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> <!-- list dependencies --> <dependencies> <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-core</artifactId> <version>${dropwizard.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <properties> <project.name>dropwizardskol</project.name> <dropwizard.version>0.9.2</dropwizard.version> <junit.version>4.12</junit.version> </properties></project>
@Path("/books")@Produces(MediaType.APPLICATION_JSON)public class BookResource { private static final Logger logger = LoggerFactory.getLogger(BookResource.class); @GET @Timed public List<Book> getBooks() { List<Book> books = new ArrayList<Book>(2); books.add(new Book("1416562605", "he White Tiger: A Novel")); books.add(new Book("1465438165", "Star Wars: The Force Awakens Visual Dictionary")); return books; } @GET @Path("{isbn}") @Timed public Book getBook(@PathParam("isbn") String isbn) { return new Book("1416562605", "he White Tiger: A Novel"); } @POST @Consumes(MediaType.APPLICATION_JSON) public void createBook(Book book) { logger.info("Enters createBook() {} > {}", book.getIsbn(), book.getTitle()); }}
Instance of Application is the main entry point for any Dropwizard application. Now our resource file has to be registered with the application like below:
public class DropwizardSkolApplication extends Application<DropwizardSkolConfiguration> { private static final String value = "Hello %s"; public static void main(String[] args) throws Exception { new DropwizardSkolApplication().run(args); } @Override public void run(DropwizardSkolConfiguration dropwizardSkolConfiguration, Environment environment) throws Exception { //Register resource BookResource bookResource = new BookResource(); environment.jersey().register(bookResource); }}
Our first REST API development is done. Finally, compile and run the application using the following commands:
mvn packagejava -jar target/dropwizardskol.jar server
Wouldn’t that be amazing? Now you can access your REST API using any client! Dropwizard is the right candidate for rapid, clean, and crisp REST API development!
I hope you have enjoyed this article. Let me know if you have any comments or feedback. Please share this article with your friends!

No comments:

Post a Comment