Dropwizard: A Practical Java Framework for Building RESTful Web Services
When you need to build a production-ready RESTful web service in Java without wading through mountains of configuration, Dropwizard is one of the most pragmatic choices available. It bundles together a set of mature, battle-tested libraries into a single, cohesive framework designed to get you from zero to a running service quickly.
What Is Dropwizard?
Dropwizard is an open-source Java framework for developing operations-friendly, high-performance RESTful web services. Rather than reinventing the wheel, it glues together best-of-breed libraries:
- Jetty for the embedded HTTP server
- Jersey for building REST APIs (JAX-RS implementation)
- Jackson for JSON serialization
- Metrics for application monitoring
- Jdbi/Hibernate for database access
- Liquibase for database migrations
- Logback for logging
The philosophy is simple: give developers a focused, opinionated stack so they can concentrate on business logic instead of infrastructure plumbing.
Why Choose Dropwizard?
- Fast startup and low overhead compared to heavier frameworks.
- Self-contained deployment as a single "fat JAR" — no external application server needed.
- Built-in operational tooling like health checks and metrics out of the box.
- Simple configuration using YAML files mapped to POJOs.
Getting Started
Maven Dependency
Add the core Dropwizard dependency to your pom.xml:
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>4.0.7</version>
</dependency>
The Configuration Class
Dropwizard applications are driven by a YAML configuration mapped to a Java class:
import io.dropwizard.core.Configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotEmpty;
public class HelloConfiguration extends Configuration {
@NotEmpty
private String greeting = "Hello, %s!";
@JsonProperty
public String getGreeting() {
return greeting;
}
@JsonProperty
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
The corresponding config.yml:
greeting: "Hello, %s!"
server:
applicationConnectors:
- type: http
port: 8080
adminConnectors:
- type: http
port: 8081
Notice the separation between the application connector (port 8080) for your API and the admin connector (port 8081) for operational endpoints.
Building a Resource
Resources are your REST endpoints, defined using standard JAX-RS annotations:
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {
private final String greetingTemplate;
public HelloResource(String greetingTemplate) {
this.greetingTemplate = greetingTemplate;
}
@GET
public Saying sayHello(@QueryParam("name") String name) {
String value = String.format(greetingTemplate,
name == null ? "Stranger" : name);
return new Saying(value);
}
}
A simple representation class for the JSON response:
public class Saying {
private final String content;
public Saying(String content) {
this.content = content;
}
@JsonProperty
public String getContent() {
return content;
}
}
Wiring It All Together
The Application class is the entry point. It bootstraps the environment and registers your resources:
import io.dropwizard.core.Application;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
public class HelloApplication extends Application<HelloConfiguration> {
public static void main(String[] args) throws Exception {
new HelloApplication().run(args);
}
@Override
public String getName() {
return "hello-world";
}
@Override
public void initialize(Bootstrap<HelloConfiguration> bootstrap) {
// Register bundles, commands, etc.
}
@Override
public void run(HelloConfiguration config, Environment environment) {
final HelloResource resource = new HelloResource(config.getGreeting());
environment.jersey().register(resource);
}
}
Running the Service
Build the fat JAR and launch it with the server command, pointing to your config:
java -jar target/hello-world-1.0.jar server config.yml
Now visit http://localhost:8080/hello?name=World and you'll get:
{"content":"Hello, World!"}
Operational Excellence: Health Checks
One of Dropwizard's standout features is its emphasis on production readiness. Health checks let you verify that your service and its dependencies are functioning:
java
import com.codahale.metrics.health.HealthCheck;
public class TemplateHealthCheck extends HealthCheck {
private final String template;
public TemplateHealthCheck(String template) {
this.template = template;
}
@Override
protected Result check() {
String test = String.format(template, "TEST");
if (!test.contains("TEST")) {
return Result
Top comments (0)