Java framework for developing ops-friendly, high-performance, RESTful web applications
—
The core application framework provides the foundation classes for building Dropwizard applications, including the main Application class, Configuration system, and runtime Environment management.
The abstract base class that serves as the main entry point for all Dropwizard applications, defining application lifecycle methods and providing command-line interface support.
package io.dropwizard.core;
public abstract class Application<T extends Configuration> {
/**
* Returns the name of the application.
*/
public abstract String getName();
/**
* Initializes the application bootstrap.
* Called before the application runs, used to register bundles and commands.
*/
public void initialize(Bootstrap<T> bootstrap) {}
/**
* Runs the application with the given configuration and environment.
* This is where you register resources, health checks, and other components.
*/
public abstract void run(T configuration, Environment environment) throws Exception;
/**
* Parses command-line arguments and runs the application.
*/
public final void run(String... arguments) throws Exception;
/**
* Main entry point for command-line execution.
*/
public static void main(String[] args) throws Exception;
}Usage Example:
public class MyApplication extends Application<MyConfiguration> {
public static void main(String[] args) throws Exception {
new MyApplication().run(args);
}
@Override
public String getName() {
return "my-service";
}
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle());
bootstrap.addCommand(new MyCustomCommand());
}
@Override
public void run(MyConfiguration configuration, Environment environment) {
final MyResource resource = new MyResource(configuration.getMessage());
environment.jersey().register(resource);
final MyHealthCheck healthCheck = new MyHealthCheck();
environment.healthChecks().register("my-health-check", healthCheck);
}
}Base configuration class that provides framework-level configuration options including server settings, logging, metrics, and administrative interface configuration.
package io.dropwizard.core;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Configuration {
@Valid
@NotNull
private ServerFactory server = new DefaultServerFactory();
@Valid
@NotNull
private LoggingFactory logging = new DefaultLoggingFactory();
@Valid
@NotNull
private MetricsFactory metrics = new DefaultMetricsFactory();
public ServerFactory getServerFactory();
public void setServerFactory(ServerFactory factory);
public LoggingFactory getLoggingFactory();
public void setLoggingFactory(LoggingFactory factory);
public MetricsFactory getMetricsFactory();
public void setMetricsFactory(MetricsFactory factory);
}Usage Example:
public class MyConfiguration extends Configuration {
@NotEmpty
private String message = "Hello, World!";
@Valid
@NotNull
private DataSourceFactory database = new DataSourceFactory();
@JsonProperty
public String getMessage() { return message; }
@JsonProperty
public void setMessage(String message) { this.message = message; }
@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() { return database; }
@JsonProperty("database")
public void setDataSourceFactory(DataSourceFactory factory) { this.database = factory; }
}The runtime container that provides access to Jersey, servlets, admin interface, lifecycle management, metrics, and health checks.
package io.dropwizard.core.setup;
public class Environment {
/**
* Returns the Jersey (JAX-RS) environment for registering resources and providers.
*/
public JerseyEnvironment jersey();
/**
* Returns the servlet environment for servlet registration.
*/
public ServletEnvironment servlets();
/**
* Returns the admin environment for administrative servlets and resources.
*/
public AdminEnvironment admin();
/**
* Returns the lifecycle environment for managing application lifecycle.
*/
public LifecycleEnvironment lifecycle();
/**
* Returns the health check registry for application health monitoring.
*/
public HealthCheckRegistry healthChecks();
/**
* Returns the metric registry for application metrics collection.
*/
public MetricRegistry metrics();
/**
* Returns the Jackson ObjectMapper for JSON serialization configuration.
*/
public ObjectMapper getObjectMapper();
/**
* Returns the validator factory for input validation.
*/
public Validator getValidator();
}Pre-start application environment used for registering bundles, commands, and configuring the application before it runs.
package io.dropwizard.core.setup;
public class Bootstrap<T extends Configuration> {
/**
* Adds a bundle to the application.
*/
public void addBundle(Bundle bundle);
/**
* Adds a configuration-aware bundle to the application.
*/
public void addBundle(ConfiguredBundle<? super T> bundle);
/**
* Adds a command to the application.
*/
public void addCommand(Command command);
/**
* Adds a configuration-aware command to the application.
*/
public void addCommand(ConfiguredCommand<T> command);
/**
* Sets the application's ObjectMapper.
*/
public void setObjectMapper(ObjectMapper objectMapper);
/**
* Sets the configuration source provider.
*/
public void setConfigurationSourceProvider(ConfigurationSourceProvider provider);
/**
* Returns the application instance.
*/
public Application<T> getApplication();
/**
* Returns the configuration class.
*/
public Class<T> getConfigurationClass();
}Usage Example:
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
// Add bundles for additional functionality
bootstrap.addBundle(new AssetsBundle("/assets/", "/static/"));
bootstrap.addBundle(new ViewBundle<>());
// Add custom commands
bootstrap.addCommand(new MyCustomCommand<>());
// Configure ObjectMapper for JSON processing
bootstrap.getObjectMapper().registerModule(new JavaTimeModule());
// Enable environment variable substitution in configuration
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(false)
)
);
}Dropwizard provides lifecycle management through the Managed interface for components that need to be started and stopped with the application.
package io.dropwizard.lifecycle;
public interface Managed {
/**
* Starts the managed object. Called when the application starts.
*/
void start() throws Exception;
/**
* Stops the managed object. Called when the application stops.
*/
void stop() throws Exception;
}Usage Example:
public class DatabaseConnectionManager implements Managed {
private Connection connection;
@Override
public void start() throws Exception {
connection = DriverManager.getConnection("jdbc:...");
}
@Override
public void stop() throws Exception {
if (connection != null) {
connection.close();
}
}
}
// Register in application
@Override
public void run(MyConfiguration configuration, Environment environment) {
environment.lifecycle().manage(new DatabaseConnectionManager());
}Monitor server lifecycle events to perform actions when the server starts, stops, or fails.
package io.dropwizard.lifecycle;
public interface ServerLifecycleListener {
/**
* Called when the server has successfully started.
*/
void serverStarted(Server server);
}Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-project