Over a million developers have joined DZone.

Dropwizard and Guice Integration

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Dropwizard, as described on their website is a "Java framework for developing ops-friendly, high-performance, RESTful web services." I have been using it recently after seeing it mentioned on my company's Yammer feed (funnily enough the framework emerged itself from Yammer) and have been enjoying it very much. One of the things I integrated into my setup of Dropwizard was Guice, as quite quickly I found I needed a way to inject my dependencies. The rest of this article will be devoted into showing you how to glue Dropwizard and Guice together.


  • Working JDK (I'm using Java 1.8)
  • Maven 3

Getting started

Start by using the Maven quickstart archetype to generate the shell of a Maven project. So in a terminal, perform the following command. Feel free to change the groupId and artifactId to suit your own purposes.

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.github.codingricky -DartifactId=DropWizardGuice -Dversion=1.0-SNAPSHOT

Now update the dependencies in the pom.xml to include what is required for this article.


This adds in the dropwizard framework, the dropwizard guiceextension and guice itself.

Adding configuration

What this demo will do, is take a message from the configuration yml file and inject it as a dependency into a resource.

Let's now add a configuration class. This will be a dropwizardconfiguration class so it will need to extend theio.dropwizard.Configuration class. Add a message field with corresponding getters/setters so dropwizard can get the message to inject into our resource.

package com.github.codingricky;

import io.dropwizard.Configuration; 
import org.hibernate.validator.constraints.NotEmpty;

public class ServerConfiguration extends Configuration { 
   private String message;
   public String getMessage() {
      return message;

Now create a config.yml and add this message property in it. Put this in the root directory of the project, so at the same level as the pom.xml file.

message: "Bonjour" 

Adding the resource

The resource in dropwizard speak represents the REST resource. It is the handler of the http request. We will create one that responds to the path/hello and it will output our message from the config.yml file. This is where Guice will get involved. Create this class in src/main/java.

package com.github.codingricky;

import com.google.inject.Inject; 
import com.google.inject.name.Named;
import javax.ws.rs.GET; 
import javax.ws.rs.Path;

public class HelloResource { 
   private final String message;

   public HelloResource(@Named("message") String message) {
      this.message = message;

   public String hello() {
      return message;

This class now expects Guice to inject a @Named property namedmessage.

Now the Guice module

Now we need a bit of glue code that takes the message from the configuration file to make it available to Guice. Create a ServerModulethat implements the com.google.inject.Module.

package com.github.codingricky;

import com.google.inject.Binder; 
import com.google.inject.Module; 
import com.google.inject.Provides;
import javax.inject.Named;

public class ServerModule implements Module { 
   public void configure(Binder binder) {

   public String provideMessage(ServerConfiguration serverConfiguration) {
      return serverConfiguration.getMessage();

Take special note that of the provideMessage that extracts the message from the ServerConfiguration.

Finally the application

Now we need to tie everything together. The application class will start the application and also will create a Guice bundle that encapsules theServerModule and the ServerConfiguration.

package com.github.codingricky;

import com.hubspot.dropwizard.guice.GuiceBundle; 
import io.dropwizard.Application; 
import io.dropwizard.setup.Bootstrap; 
import io.dropwizard.setup.Environment;

public class ServerApplication extends Application<ServerConfiguration> { 
   public void initialize(Bootstrap<ServerConfiguration> bootstrap) {
   GuiceBundle<ServerConfiguration> guiceBundle =       GuiceBundle.<ServerConfiguration>newBuilder()
      .addModule(new ServerModule())

   public void run(ServerConfiguration configuration, Environment environment) throws    Exception {

   public static void main(String[] args) throws Exception {
      new ServerApplication().run(args);

Let's Go!

The final step is to package everything up and run it.

So add in the plugin details that will package up the application and allow it to be run via the commandline.

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

Now we can build everything via Maven.

$ mvn clean install

Finally we can start the application

$ java -jar target/DropWizardGuice-1.0-SNAPSHOT.jar server config.yml

If the application starts up correctly, you should be able to go to a browser and hit the url, http://localhost:8080/hello


Thanks for reading, I hope this article was useful to you. Here are some references that were used in this article.

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.


Published at DZone with permission of Ricky Yim , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}