Dropwizard is a Java framework for developing ops-friendly, high-performance, RESTful web services.
Dropwizard pulls together stable, mature libraries from the Java ecosystem into a simple, light-weight package that lets you focus on getting things done.
Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality web service in the shortest time possible.
#server configuration
server:
applicationConnectors:
- type: http
port: 8080
- type: https
port: 8443
keyStorePath: example.keystore
keyStorePassword: example
validateCerts: false
validatePeers: false
fancyCoolProperty: Look at me I am cool and fancy
# Database settings.
database:
# the name of your JDBC driver
driverClass: org.h2.Driver
# the username
user: sa
# the password
password: sa
# the JDBC URL
url: jdbc:h2:./target/example
Configuration class
Java
public class ApplicationConfiguration extends Configuration {
@NotEmpty
private String fancyCoolProperty;
@JsonProperty
public String getFancyCoolProperty() {
return fancyCoolProperty;
}
@JsonProperty
public void setFancyCoolProperty(String fancyCoolProperty) {
this.fancyCoolProperty = fancyCoolProperty;
}
}
public class HelloWorldApplication extends Application<ApplicationConfiguration> {
@Override
public String getName() {
return "hello-world";
}
@Override
public void initialize(Bootstrap<ApplicationConfiguration> bootstrap) {
// nothing to do yet
}
@Override
public void run(ApplicationConfiguration configuration, Environment environment) {
// nothing to do yet
}
}
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
@GET
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = name.orElse("unkown");
return new Saying(value);
}
}
Registering a resource
public void run(ApplicationConfiguration configuration, Environment environment) {
final HelloWorldResource resource = new HelloWorldResource();
environment.jersey().register(resource);
}
A health check is basically a small self-test which your application performs to verify that a specific component or responsibility is performing correctly.
Health check
public class SimpleHealthCheck extends HealthCheck {
@Override
protected Result check() throws Exception {
//check some stuff here!
return Result.healthy("Everything is fine");
}
}
Registering a health check
public void run(ApplicationConfiguration configuration, Environment environment) {
final SimpleHealthCheck healthCheck = new SimpleHealthCheck();
environment.healthChecks().register("simpleCheck", healthCheck);
}
Running your application
Do you remember the Java main() method?
public static void main(final String[] args) throws Exception {
final HelloWorldApplication application = new HelloWorldApplication();
application.run(args);
}