Core components of the Dropwizard framework for building Java web applications
—
The core application framework provides the essential classes for building Dropwizard applications, including the main Application class, Configuration base class, and Bootstrap setup.
The abstract base class that all Dropwizard applications must extend. Provides application lifecycle management, command registration, and configuration handling.
/**
* The base class for Dropwizard applications.
* @param <T> the type of configuration class for this application
*/
public abstract class Application<T extends Configuration> {
/**
* Returns the name of the application.
* @return the application's name
*/
public String getName();
/**
* Returns the Class of the configuration class type parameter.
* @return the configuration class
*/
public Class<T> getConfigurationClass();
/**
* Initializes the application bootstrap.
* @param bootstrap the application bootstrap
*/
public void initialize(Bootstrap<T> bootstrap);
/**
* When the application runs, this is called after the bundles are run.
* Override to add providers, resources, etc. for your application.
* @param configuration the parsed Configuration object
* @param environment the application's Environment
* @throws Exception if something goes wrong
*/
public abstract void run(T configuration, Environment environment) throws Exception;
/**
* Parses command-line arguments and runs the application.
* Call this method from a public static void main entry point.
* @param arguments the command-line arguments
* @throws Exception if something goes wrong
*/
public void run(String... arguments) throws Exception;
/**
* The log level at which to bootstrap logging on application startup.
* @return the bootstrap log level (default: Level.WARN)
*/
protected Level bootstrapLogLevel();
/**
* Called by run(String...) to add the standard "server" and "check" commands
* @param bootstrap the bootstrap instance
*/
protected void addDefaultCommands(Bootstrap<T> bootstrap);
/**
* Called by run(String...) to indicate there was a fatal error running the requested command.
* @param t The Throwable instance which caused the command to fail
*/
protected void onFatalError(Throwable t);
}Usage Examples:
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) {
// Add bundles during initialization
bootstrap.addBundle(new AssetsBundle("/assets/", "/", "index.html"));
bootstrap.addBundle(new SslReloadBundle());
}
@Override
public void run(MyConfiguration configuration, Environment environment) {
// Register resources
environment.jersey().register(new MyResource());
// Register health checks
environment.healthChecks().register("database", new DatabaseHealthCheck());
// Register admin tasks
environment.admin().addTask(new MyAdminTask());
}
}Base configuration class that applications extend to define their configuration schema. Supports YAML-based configuration with Jackson binding and Bean Validation.
/**
* An object representation of the YAML configuration file.
* Extend this with your own configuration properties.
*/
public class Configuration {
/**
* Returns the server-specific section of the configuration file.
* @return server-specific configuration parameters
*/
@JsonProperty("server")
public ServerFactory getServerFactory();
/**
* Sets the HTTP-specific section of the configuration file.
* @param factory the server factory
*/
@JsonProperty("server")
public void setServerFactory(ServerFactory factory);
/**
* Returns the logging-specific section of the configuration file.
* @return logging-specific configuration parameters
*/
@JsonProperty("logging")
public synchronized LoggingFactory getLoggingFactory();
/**
* Sets the logging-specific section of the configuration file.
* @param factory the logging factory
*/
@JsonProperty("logging")
public synchronized void setLoggingFactory(LoggingFactory factory);
/**
* Returns the metrics-specific section of the configuration file.
* @return metrics configuration parameters
*/
@JsonProperty("metrics")
public MetricsFactory getMetricsFactory();
/**
* Sets the metrics-specific section of the configuration file.
* @param metrics the metrics factory
*/
@JsonProperty("metrics")
public void setMetricsFactory(MetricsFactory metrics);
/**
* Returns the admin interface-specific section of the configuration file.
* @return admin interface-specific configuration parameters
*/
@JsonProperty("admin")
public AdminFactory getAdminFactory();
/**
* Sets the admin interface-specific section of the configuration file.
* @param admin the admin factory
*/
@JsonProperty("admin")
public void setAdminFactory(AdminFactory admin);
/**
* Returns the health interface-specific section of the configuration file.
* @return health interface-specific configuration parameters
*/
@JsonProperty("health")
public Optional<HealthFactory> getHealthFactory();
/**
* Sets the health interface-specific section of the configuration file.
* @param health the health factory
*/
@JsonProperty("health")
public void setHealthFactory(HealthFactory health);
}Usage Examples:
public class MyConfiguration extends Configuration {
@NotEmpty
private String databaseUrl;
@Min(1)
@Max(100)
private int connectionPoolSize = 10;
@Valid
@NotNull
private RedisConfiguration redis = new RedisConfiguration();
@JsonProperty
public String getDatabaseUrl() {
return databaseUrl;
}
@JsonProperty
public void setDatabaseUrl(String databaseUrl) {
this.databaseUrl = databaseUrl;
}
@JsonProperty
public int getConnectionPoolSize() {
return connectionPoolSize;
}
@JsonProperty
public void setConnectionPoolSize(int connectionPoolSize) {
this.connectionPoolSize = connectionPoolSize;
}
@JsonProperty
public RedisConfiguration getRedis() {
return redis;
}
@JsonProperty
public void setRedis(RedisConfiguration redis) {
this.redis = redis;
}
}Configuration is defined in YAML format and automatically parsed:
# Server configuration
server:
type: default
applicationConnectors:
- type: http
port: 8080
adminConnectors:
- type: http
port: 8081
# Logging configuration
logging:
level: INFO
loggers:
com.example: DEBUG
# Custom application configuration
databaseUrl: "jdbc:postgresql://localhost/mydb"
connectionPoolSize: 20
redis:
host: localhost
port: 6379// Log level enumeration used by bootstrapLogLevel()
public enum Level {
ERROR, WARN, INFO, DEBUG, TRACE
}Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-core