Java framework for developing ops-friendly, high-performance, RESTful web applications
npx @tessl/cli install tessl/maven-io-dropwizard--dropwizard-project@3.0.0Dropwizard is a Java framework for developing ops-friendly, high-performance, RESTful web applications. It brings together mature Java libraries including Jetty, Jersey, Jackson, Logback, Hibernate Validator, and Metrics into a cohesive framework that enables rapid development of production-ready services with built-in operational features.
pom.xmlimport io.dropwizard.core.Application;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;Additional common imports:
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import com.codahale.metrics.annotation.Timed;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;// 1. Create configuration class
public class HelloWorldConfiguration extends Configuration {
@NotNull
private String template = "Hello, %s!";
@JsonProperty
public String getTemplate() { return template; }
@JsonProperty
public void setTemplate(String template) { this.template = template; }
}
// 2. Create JAX-RS resource
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
private final String template;
public HelloWorldResource(String template) {
this.template = template;
}
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.orElse("Stranger"));
return new Saying(value);
}
}
// 3. Create application class
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) {
// Add bundles here
}
@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate()
);
environment.jersey().register(resource);
}
}Dropwizard follows a convention-over-configuration approach with several key architectural components:
Application<T> that defines application lifecycleFoundation classes for building Dropwizard applications including the main Application class, Configuration system, and runtime Environment management.
public abstract class Application<T extends Configuration> {
public abstract String getName();
public void initialize(Bootstrap<T> bootstrap) {}
public abstract void run(T configuration, Environment environment) throws Exception;
public final void run(String... arguments) throws Exception;
}
public class Configuration {
// Server configuration, logging, metrics, admin interface setup
}
public class Environment {
public JerseyEnvironment jersey();
public ServletEnvironment servlets();
public AdminEnvironment admin();
public LifecycleEnvironment lifecycle();
public HealthCheckRegistry healthChecks();
public MetricRegistry metrics();
}Jersey (JAX-RS) integration for building RESTful web services with automatic JSON serialization, validation, and metrics collection.
public class JerseyEnvironment {
public void register(Object component);
public void register(Class<?> componentClass);
public void packages(String... packages);
}
// Common JAX-RS annotations for resources
@Path("/resource")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ResourceClass {
@GET @POST @PUT @DELETE
@Timed @Metered @Counted
public ResponseType method(@PathParam("id") String id,
@QueryParam("param") String param,
@Valid RequestType request);
}YAML-based configuration system with environment variable substitution, validation, and type-safe access to application settings.
public class ConfigurationFactory<T> {
public T build(ConfigurationSourceProvider provider, String path) throws IOException, ConfigurationException;
}
public interface ConfigurationSourceProvider {
InputStream open(String path) throws IOException;
}
// Configuration validation annotations
@NotNull @NotEmpty @Valid @Min @Max @Range
@DurationRange @DataSizeRange @OneOf @PortRangePluggable authentication system supporting basic auth, OAuth, and custom authentication schemes with fine-grained authorization control.
public interface Authenticator<C, P> {
Optional<P> authenticate(C credentials) throws AuthenticationException;
}
public interface Authorizer<P> {
boolean authorize(P principal, String role);
}
public class AuthDynamicFeature implements DynamicFeature {
// Register authentication filters with Jersey
}Comprehensive database support including connection pooling, ORM integration with Hibernate, lightweight SQL access with JDBI, and database migrations with Liquibase.
public class DataSourceFactory {
// Database connection configuration
}
public class HibernateBundle<T extends Configuration> implements ConfiguredBundle<T> {
// Hibernate ORM integration
}
public class MigrationsBundle<T extends Configuration> implements ConfiguredBundle<T> {
// Liquibase database migrations
}Built-in application metrics collection, health checks, and operational endpoints for production monitoring and observability.
public class MetricRegistry {
public Timer timer(String name);
public Counter counter(String name);
public Meter meter(String name);
public Histogram histogram(String name);
}
public abstract class HealthCheck {
public abstract Result check() throws Exception;
}
// Metric annotations
@Timed @Metered @Counted @GaugeComprehensive input validation using Bean Validation (JSR-303) with custom Dropwizard validators for durations, data sizes, and other common types.
// Built-in validation annotations
@DurationRange(min = 1, minUnit = TimeUnit.SECONDS, max = 1, maxUnit = TimeUnit.HOURS)
@DataSizeRange(min = 1, minUnit = DataSize.Unit.KILOBYTES)
@OneOf({"value1", "value2", "value3"})
@PortRange(min = 1024, max = 65535)
@ValidationMethod(message = "Custom validation failed")Comprehensive testing utilities including JUnit 5 extensions for full application testing, resource testing, and database testing.
public class DropwizardAppExtension<C extends Configuration> implements BeforeAllCallback, AfterAllCallback {
// Full application testing
}
public class ResourceExtension implements BeforeEachCallback, AfterEachCallback {
// Resource-level testing
}
public class DAOTestExtension implements BeforeEachCallback, AfterEachCallback {
// Database testing support
}Dropwizard's bundle system provides reusable application components that can configure multiple aspects of an application. Bundles are registered during the bootstrap phase and can modify both bootstrap and runtime configuration.
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/", "/static/"));
bootstrap.addBundle(new ViewBundle<MyConfiguration>());
bootstrap.addBundle(new MigrationsBundle<MyConfiguration>() {
@Override
public DataSourceFactory getDataSourceFactory(MyConfiguration configuration) {
return configuration.getDatabase();
}
});
}