Introduction to Dropwizard

What is Dropwizard

Treat it as a library or a framework.
To provide implementations of everything a production-ready web application needs.

Components

  1. Application
  2. Configuration
  3. Representation
  4. Resource
  5. Health Check
    Dropwizard

Application

(HelloWorldApplication.java): startup the app.

1, static main is app’s entry point.
2, Register resource: in run method, 1,new resource instance 2,add to Jersey env.
3, read configuration from HelloWorldConfiguration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.example.helloworld;

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
new HelloWorldApplication().run(args);
}

@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
// configure aspects of the application required before the application is run
// like bundles, configuration source providers
}

@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
// REGISTER RESOURCE
environment.jersey().register(resource);
}

}

Configuration

(HelloWorldConfiguration.java): environment configuration.
Parameters are specified in a YAML configuration file.
Dropwizard parses the provided YAML configuration file and builds an instance of app configuration class by mapping YAML field names to object field names.

Representation

(Saying.java): API Data Structure.
Request content:

1
2
3
4
{
"id": 1,
"content": "Hi!"
}

Translate to representation class:
Jackson to serialize/deserialize @JsonProperty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.helloworld.api;

public class Saying {
private long id;
private String content;

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

@JsonProperty
public long getId() {
return id;
}

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

Resource

(HelloWordResource.java): service implementation entry point
@Path (URI: /hello-world) and @Produces (Jersey’s content negotiation code: application/json)

request /hello-world?name=Dougie: sayHello method will be called with Optional.of("Dougie").

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.helloworld.resources;

@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
private final String defaultName;

public HelloWorldResource(String defaultName) {
this.defaultName = defaultName;
}

@GET
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.orElse(defaultName));
return new Saying(counter.incrementAndGet(), value);
}
}

Health Check

(TemplateHealthCheck.java): runtime tests that show if the app still works.

Bootstrap

Bootstrap is the the pre-start(temp) application environment, containing everything required to bootstrap a Dropwizard command.

Before a Dropwizard application can provide the command-line interface, parse a configuration file, it must first go through a bootstrapping phase.

This phase corresponds to initialize method. You can add Bundles, or register Jackson modules to include custom types as part of your configuration class.

Environments

A Dropwizard Environment consists of all the Resources, servlets, filters, Health Checks, Jersey providers, Managed Objects, Tasks, and Jersey properties.

Application class run method: where to create new resource instances, etc., and adding them to the given Environment class

Environment is a longer-lived object, holding Dropwizard’s Environment (not env. Such as dev or prod). It holds a similar, but somewhat different set of properties than the Bootsrap object.

References

http://www.dropwizard.io/en/stable/getting-started.html
https://xebia.com/blog/dropwizard/