Introduction to Dropwizard


@giftkugel

Simon Skoczylas Senior Software Developer
Materna GmbH

Java, JavaScript
Web, Frontend

What’s a Dropwizard?

A character in a K.C. Green web comic.

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.

Best-of-breed Java libraries

Best-of-breed Java libraries

More modules

http://modules.dropwizard.io

Releases

  • First release v0.1.0 in 2011 by Coda Hale at Yammer
  • v0.7.0 dropped Scala support (04.04.2014)
  • Release v1.0.0 (26.07.2016)
    • Using Java 8 as baseline
  • Current release v1.0.2 (23.09.2016)

Main components

  • Configuration
  • Application
  • Representation
  • Resource
  • Health check

Configuration

Configuration file

YAML

#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;
}
}
	

Application

Application class


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
}

}
	

Representation

Representation


public class Saying {

private String content;

public Saying() {
// Jackson deserialization
}

public Saying(String content) {
this.content = content;
}

@JsonProperty
public String getContent() {
return content;
}

}
	

Resource

  • The meat-and-potatoes of Dropwizard
  • JAX-RS API (Jersey)

Resource


@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);

}
	

Health check

Provided by Dropwizard Metrics
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);

}
	

Running your application

  • Within the IDE
  • As Maven or Gradle task
  • As self contained jar
    • Make features not war (JAR not WAR)
    • java -jar application.jar server application.yml

Dropwizard is used by

Some Competitors

Ratpack, Ninja framework, ...

Questions?

// http://kangax.github.io/compat-table/es6/ // Verwendbar über Featuredetection und Polyfills