Core components of the Dropwizard framework for building Java web applications
npx @tessl/cli install tessl/maven-io-dropwizard--dropwizard-core@4.0.0Dropwizard Core provides the fundamental building blocks for creating production-ready Java web applications. It combines Jetty, Jersey, Jackson, and other proven libraries into a cohesive framework that enables rapid development of RESTful web services with built-in metrics, logging, health checks, and operational features.
pom.xml:
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>4.0.14</version>
</dependency>import io.dropwizard.core.Application;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;import io.dropwizard.core.Application;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
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 to bootstrap
}
@Override
public void run(HelloWorldConfiguration configuration, Environment environment) {
// Register resources, health checks, tasks, etc.
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
environment.jersey().register(resource);
}
}
public class HelloWorldConfiguration extends Configuration {
@NotEmpty
private String template = "Hello, %s!";
@NotEmpty
private String defaultName = "Stranger";
@JsonProperty
public String getTemplate() {
return template;
}
@JsonProperty
public void setTemplate(String template) {
this.template = template;
}
@JsonProperty
public String getDefaultName() {
return defaultName;
}
@JsonProperty
public void setDefaultName(String name) {
this.defaultName = name;
}
}Dropwizard Core is built around several key components:
Application class serves as the main entry point with configurable bootstrapping and runtime phasesEnvironment class provides unified access to Jersey, servlet, admin, lifecycle, and health subsystemsEssential classes for building Dropwizard applications including the main Application class, Configuration base class, and Bootstrap setup.
public abstract class Application<T extends Configuration> {
public abstract void run(T configuration, Environment environment) throws Exception;
public void initialize(Bootstrap<T> bootstrap);
public void run(String... arguments) throws Exception;
}
public class Configuration {
public ServerFactory getServerFactory();
public LoggingFactory getLoggingFactory();
public MetricsFactory getMetricsFactory();
public AdminFactory getAdminFactory();
public Optional<HealthFactory> getHealthFactory();
}Environment management providing access to Jersey, servlet container, admin interface, lifecycle management, and health checks.
public class Environment {
public JerseyEnvironment jersey();
public ServletEnvironment servlets();
public AdminEnvironment admin();
public LifecycleEnvironment lifecycle();
public HealthEnvironment health();
public MetricRegistry metrics();
public HealthCheckRegistry healthChecks();
}
public class Bootstrap<T extends Configuration> {
public void addBundle(ConfiguredBundle<? super T> bundle);
public void addCommand(Command command);
public MetricRegistry getMetricRegistry();
public ObjectMapper getObjectMapper();
}Extensible command-line interface system with built-in commands for server management and configuration validation.
public abstract class Command {
public Command(String name, String description);
public abstract void configure(Subparser subparser);
public abstract void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception;
}
public abstract class ConfiguredCommand<T extends Configuration> extends Command {
protected abstract void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception;
}Pluggable server factory system supporting different deployment patterns with configurable connectors and thread pools.
public interface ServerFactory extends Discoverable {
Server build(Environment environment);
void configure(Environment environment);
}
public class DefaultServerFactory extends AbstractServerFactory {
// Multi-connector server supporting separate application and admin ports
}
public class SimpleServerFactory extends AbstractServerFactory {
// Single-connector server combining application and admin on same port
}Reusable application components and built-in bundles for common functionality like SSL certificate reloading.
public interface ConfiguredBundle<T> {
default void run(T configuration, Environment environment) throws Exception;
default void initialize(Bootstrap<?> bootstrap);
}
public class SslReloadBundle implements ConfiguredBundle<Configuration> {
// Provides SSL certificate reloading capability via admin task
}// Base configuration class
public class Configuration {
@Valid @NotNull
private ServerFactory server = new DefaultServerFactory();
@Valid @Nullable
private LoggingFactory logging;
@Valid @NotNull
private MetricsFactory metrics = new MetricsFactory();
@Valid @NotNull
private AdminFactory admin = new AdminFactory();
@Valid @Nullable
private HealthFactory health;
}
// ConfiguredBundle interface for reusable components
public interface ConfiguredBundle<T> {
default void run(T configuration, Environment environment) throws Exception;
default void initialize(Bootstrap<?> bootstrap);
}
// Server factory interface for pluggable server implementations
public interface ServerFactory extends Discoverable {
Server build(Environment environment);
void configure(Environment environment);
}