DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Integration Architecture Guiding Principles, A Reference
  • Comparing Cloud Hosting vs. Self Hosting
  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • How To Scan and Validate Image Uploads in Java

Trending

  • Integration Architecture Guiding Principles, A Reference
  • Comparing Cloud Hosting vs. Self Hosting
  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • How To Scan and Validate Image Uploads in Java
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Dropwizard and Guice Integration

Dropwizard and Guice Integration

Ricky Yim user avatar by
Ricky Yim
·
Aug. 06, 14 · Interview
Like (1)
Save
Tweet
Share
25.98K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Prerequisites

  • 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.

 <dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>com.hubspot.dropwizard</groupId>
<artifactId>dropwizard-guice</artifactId>
<version>0.7.0.2</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
</dependencies>

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 { 
   @NotEmpty
   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;

@Path("/hello")
public class HelloResource { 
   private final String message;

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

   @GET
   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 { 
   @Override
   public void configure(Binder binder) {
   }

   @Provides
   @Named("message")
   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> { 
   @Override
   public void initialize(Bootstrap<ServerConfiguration> bootstrap) {
   GuiceBundle<ServerConfiguration> guiceBundle =       GuiceBundle.<ServerConfiguration>newBuilder()
      .addModule(new ServerModule())
      .setConfigClass(ServerConfiguration.class)
      .enableAutoConfig(getClass().getPackage().getName())
      .build();
      bootstrap.addBundle(guiceBundle);
   }

   @Override
   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.

<plugins> 
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.github.codingricky.ServerApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

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

Summary

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

  • https://dropwizard.github.io/dropwizard/
  • https://github.com/HubSpot/dropwizard-guice
Integration application

Published at DZone with permission of Ricky Yim, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Integration Architecture Guiding Principles, A Reference
  • Comparing Cloud Hosting vs. Self Hosting
  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • How To Scan and Validate Image Uploads in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: