Rapid REST API Development With Dropwizard
Learn how to create your own REST API with Java and Dropwizard with this practical tutorial.
Join the DZone community and get the full member experience.
Join For FreeAs 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 straightforward? Probably NOT!
But one framework I evaluated recently has most of the plus-points, and it allowed me to focus on my business problem instead of the plumbing work to prepare the infrastructure. This tool is none other than “Dropwizard.”
This tiny framework came very late in the market when compared to most of the other frameworks, but it attracted developers' attention in a shorter time. In 2012 it was mentioned in the Thoughtworks technology radar as a “TRIAL.” It jumped to “ADAPT” within 2 years (see 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 developer BOX. It contains all the good parts required for easy development. For example, it contains the following well-known APIs/frameworks to ease our development.
- REST API – Jersey
- Logging – Logback
- Deployment – Jetty
- Metrics – Metrics library
- Health check – Built -in
- Collection Util – Google Guava
- JSON Parsing – Jackson library
- and many more…
Ok, we have seen some of the good parts of the Dropwizard framework. In the next section we will 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 classDropwizardSkolApplication 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=newBookResource();
environment.jersey().register(bookResource);
}
}
Our first REST API development is done. Finally, compile and run the application using the following commands:
mvn package
java -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 clean and crisp RAPID 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…
Published at DZone with permission of Thamizh Arasu, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments