This document will guide you through the process of creating
a simple Dropwizard project: Hello World.
Overview:
Dropwizard is a Java framework for developing ops-friendly,
high-performance, RESTful web services. This framework bundles with Jetty for
http sever, Jersey for REST and Jackson for JSON mapping. It offers some additional few other additional features like Metrics.
Setting up Maven:
I recommend you use
Maven for new Dropwizard applications. Current version of Dropwizard 0.8.1.
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.8.0</version>
</dependency>
Creating A Configuration Class
Each Dropwizard application has its own subclass of the
Configuration class which specifies environment-specific parameters. These
parameters are specified in a YAML configuration file which is deserialized to
an instance of your application’s configuration class and validated
import
io.dropwizard.Configuration;
public class
HelloWorldConfiguration extends Configuration {
// Add your own
configuration variables
}
Creating a Representation class:
This class will be look like Model class and it designed for generating the JSON response. Representation class will be look like below.
import
com.fasterxml.jackson.annotation.JsonProperty;
import
org.hibernate.validator.constraints.Length;
public class Saying {
private long id;
@Length(max = 3)
private String content;
public Saying() {
}
public Saying(long id, String content) {
this.id = id;
this.content = content;
}
@JsonProperty
public long getId() {
return id;
}
@JsonProperty
public String getContent() {
return content;
}
}
Creating Resource
Class:
You need a resource which returns Saying instances from the
URI /hello-world, so our resource class will look like this:
import
javax.ws.rs.GET;
import
javax.ws.rs.Path;
import
javax.ws.rs.Produces;
import
javax.ws.rs.QueryParam;
import
javax.ws.rs.core.MediaType;
import model.Saying;
import
java.util.concurrent.atomic.AtomicLong;
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class
HelloWorldResource {
private final AtomicLong counter;
public HelloWorldResource() {
this.counter = new AtomicLong();
}
@GET
public Saying
sayHello(@QueryParam("name") String name) {
final String value = name;
return new
Saying(counter.incrementAndGet(), value);
}
}
Creating An Application Class
Application going to be the Main class for your application.
It will register your resource class. It will be look like below.
import
configuration.HelloWorldConfiguration;
import
io.dropwizard.Application;
import
io.dropwizard.setup.Bootstrap;
import
io.dropwizard.setup.Environment;
import
resources.HelloWorldResource;
public class
HelloWorldApplication extends Application<HelloWorldConfiguration> {
public static void main(String[] args)
throws Exception {
new HelloWorldApplication().run(args);
}
@Override
public String getName() {
return "hello-world";
}
@Override
public void
initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
// nothing to do yet
}
@Override
public void run(HelloWorldConfiguration
configuration,
Environment environment) {
final
HelloWorldResource resource = new HelloWorldResource();
environment.jersey().register(resource);
}
}
Create a YMAL file and place it your resource folder. It
will be look like below
server:
# softNofileLimit: 1000
# hardNofileLimit: 1000
applicationConnectors:
- type: http
port: 8085
# this requires the
alpn-boot library on the JVM's boot classpath
# - type: spdy3
# port: 8445
# keyStorePath: example.keystore
# keyStorePassword: example
# validateCerts: false
adminConnectors:
- type: http
port: 8086
Finally the run the Application class which will have main method from your eclipse and pass
the below parameters to your main class.
server <YML_file_path>