Dropwizard provides a great infrastructure for creating microservices. Jetty for http, Jersey for Rest, Jackson for JSON, Metrics for metrics and other useful helper libraries can be seen as a “best of breed” approach for developing microservices with the java platform. The usage of CDI in web- and backend applications however has been essential in the last couple of projects I worked on and I surely do not want to miss it in microservice projects.

There are several java CDI libraries that can be integrated into a dropwizard application

  • OpenWebBeans (implements the JSR specification for CDI)
  • Weld (implements the JSR specification for CDI)
  • Google Guice (Googles CDI approach)
  • Spring (origin of all CDI approaches)

I have checked out several options to integrate CDI into a dropwizard project and came up that using standard Spring was the best choice for me. While integrating OpenWebBeans and Weld did not work “out-of-the-box” for me and these frameworks had a lot of dependencies I did not want to have in a dropwizard project, there is already a “dropwizard-guice” bridge that I could use. After some attempts it turned out to only support constructor and method injection, I was searching for an opportunity to use field injection in my code.

The integration of spring was really straightforward and came in just 2 easy steps:

Integrate spring-context into your dropwizard project (as you can see, I used maven as dependency management system, other frameworks will work as well). The commons-logging library can be excluded from the project as dropwizard uses slf4j as logging framework:


	io.dropwizard
	dropwizard-core
	${dropwizard.version}


	org.springframework
	spring-context
	${spring.version}
	
		
			commons-logging
			commons-logging
		
	

Everything you have to do in the java code is to create a spring ConfigurationContext and pass the beans of resource classes that are created by this context to the jersey framework, that handles all rest calls. The application class of your dropwizard project has a run method where your spring context should be created:

@Override
public void run(ServiceConfiguration configuration, Environment environment) {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan(this.getClass().getPackage().getName());
	context.refresh();
	environment.jersey().register(context.getBean(DocumentResource.class));
}

The packages are scanned automatically for java classes with valid spring annotations for CDI managed beans. With this peace of code you can use the common spring CDI annotations like @Service (for backend database services) or @Component (for Rest resource classes) and their instances will be injected with the @Autowired annotation that you know from your spring projects.

A small sample project with all the code can be downloaded from bitbucket: https://bitbucket.org/mekaso/winservice

The project is a microservice with two methods

  • findAllDocuments() to find all 3 documents from a backend
  • findDocumentById(long id) to find one document in the list of all documents

After starting your microservice with java –jar .\target\WinService-1.0-SNAPSHOT.jar server service-8080.yml you can access the service by calling the urls http://localhost:8080/documents or http://localhost:8080/documents/2 .

 

By Meik Kaufmann

I am a certified oracle enterprise architect and like to share my knowledge and experience that I gain in the projects I am working on.

Leave a Reply

Your email address will not be published. Required fields are marked *