- Java: Basic Dropwizard Project
- Dropwizard: Guice Bundle
- Dropwizard: Command
- Dropwizard: Resource
- Dropwizard: Swagger Integration
In this tutorial I will show you how to add Guice to your Dropwizard app. This will be a very basic implementation. Some things you should note is that I didn’t put in any docstrings. You should always do that!
Now there are a few Dropwizard Guice integrations available but the most active is the one I will show you today called “dropwizard-guicey“.
POM.xml
<dependency> <groupId>ru.vyarus</groupId> <artifactId>dropwizard-guicey</artifactId> <version>4.1.0</version> </dependency>
Model
Now we create a model to use with our service
package ca.gaudreault.mydropwizardapp.models; import java.io.Serializable; import javax.validation.constraints.NotNull; public class MyModel implements Serializable { private static final long serialVersionUID = 1L; private Integer value; public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } }
Service
Here you will create your service interface and class so that you can bind it in the guice module.
Interface
package ca.gaudraeult.mydropwizardapp.services; import ca.gaudreault.mydropwizardapp.models.MyModel; public interface MyService { MyModel runTest(); }
Implementation
package ca.gaudraeult.mydropwizardapp.services; import ca.gaudreault.mydropwizardapp.models.MyModel; public class MyServiceImpl implements MyService { public MyServiceImpl() { } @Override public MyModel runTest() { final MyModel myModel = new MyModel(); myModel.setValue(123123); return myModel; } }
ServerModule
Now when we create our module class you can bind the interface to the implementation. Note that if your implementation does not implement the interface this will not work.
package ca.gaudreault.mydropwizardapp; import com.google.inject.AbstractModule; import ca.gaudraeult.mydropwizardapp.services.MyService; import ca.gaudraeult.mydropwizardapp.services.MyServiceImpl; public class ServerModule extends AbstractModule { @Override protected void configure() { bind(MyService.class).to(MyServiceImpl.class); } }
Dropwizard Application
If you remember from part 1 of this series you created the based Dropwizard app. So you should have a class called “MyDropwizardAppApplication”. Open that now and modify the “initialize” like the below. Baseically here we are registering our ServerModule class to Dropwizard.
@Override public void initialize(final Bootstrap bootstrap) { bootstrap.addBundle(GuiceBundle.builder() .enableAutoConfig(this.getClass().getPackage().getName()) .modules(new ServerModule()) .build()); }
And that is it you have configured a very basic Dropwizard Guice configuration.
2 thoughts on “Dropwizard: Guice Bundle”
Comments are closed.