Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ dropwizard-spring
With **dropwizard-spring** you configure your dropwizard components (resources, healthchecks, jersey providers, managed objects, servlets, filters, etc.)
entirely with spring, and tell the spring service which components to enable using a simple YAML configuration.

With **dropwizard-spring** it is not necessary to subclass `com.yammer.dropwizard.Service`, instead you reference the provided
`com.hmsonline.dropwizard.spring.SpringService` class as your service class.
With **dropwizard-spring** it is not necessary to subclass `io.dropwizard.Application`, instead you reference the provided
`com.hmsonline.dropwizard.spring.SpringService` class as your application class.

With **dropwizard-spring** you can use XML or annotation based (Java Based Config) spring bean definitions

To see **dropwizard-spring** in action look into the example folder of this project

## Maven Configuration

Expand Down Expand Up @@ -107,6 +110,11 @@ This is required to have maven build a "fat," executable jar file.
# The location of one or more beans.xml files
configLocations:
- conf/dropwizard-beans.xml

# Package to look for JavaConfig classes annotated with @Configuration
# Set either javaConfigBasePackages or configLocations
javaConfigBasePackages:
- com.yourpackage.spring.config

# Servlet Filter
# List of FilterConfiguration
Expand Down
8 changes: 8 additions & 0 deletions example/java-config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
appContextType: app

javaConfigBasePackages:
- com.hmsonline.dropwizard.spring.config

resources:
- helloResource
160 changes: 160 additions & 0 deletions example/java-config/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.0.0</maven>
</prerequisites>

<groupId>com.hmsonline</groupId>
<artifactId>dropwizard-spring-javaconfig-example</artifactId>
<version>0.6.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Dropwizard-Spring Java Config Example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mainClass>com.hmsonline.dropwizard.spring.SpringService</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>com.hmsonline</groupId>
<artifactId>dropwizard-spring</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainClass}</mainClass>
</transformer>
<!-- must be SURE to do this with both spring.handlers and spring.schemas.
otherwise you won't be able to use them in the spring config files. -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
<!-- exclude signed Manifests -->
<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>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<configuration>
<reportPlugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.4</version>
<configuration>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
<dependencyDetailsEnabled>false</dependencyDetailsEnabled>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>

</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hmsonline.dropwizard.spring;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {

public String greet(String name) {
return "Hi " + name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2012 Health Market Science, Inc.

package com.hmsonline.dropwizard.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Component
@Path("/hello")
public class HelloResource {

@Autowired
private MessageBean messageBean;

@Autowired
private GreetingService greetingService;

@GET
@Path("/world")
@Produces(MediaType.APPLICATION_JSON)
public Object hello(){
return greetingService.greet(messageBean.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hmsonline.dropwizard.spring;

import org.springframework.stereotype.Component;

public class MessageBean {

private String message;

public MessageBean(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hmsonline.dropwizard.spring.config;

import com.hmsonline.dropwizard.spring.MessageBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.hmsonline.dropwizard.spring")
public class Config {

@Bean
MessageBean createMessageBean() {
return new MessageBean("Java Config");
}

}
6 changes: 6 additions & 0 deletions example/java-config/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
================================================================================

dropwizard-javaconfig-spring-example

================================================================================

File renamed without changes.
2 changes: 1 addition & 1 deletion example/pom.xml → example/xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.hmsonline</groupId>
<artifactId>dropwizard-spring-example</artifactId>
<version>0.2.1-SNAPSHOT</version>
<version>0.6.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Dropwizard-Spring Example</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hmsonline.dropwizard.spring;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.IOException;
import java.text.MessageFormat;

public final class ApplicationContextTypeDeserializer extends JsonDeserializer<SpringConfiguration.ApplicationContextType> {

@Override
public SpringConfiguration.ApplicationContextType deserialize(final JsonParser parser, final DeserializationContext context) throws IOException, JsonProcessingException {
final String jsonValue = parser.getText();

for (SpringConfiguration.ApplicationContextType applicationContextType : SpringConfiguration.ApplicationContextType.values()) {
if (applicationContextType.getName().equals(jsonValue)) {
return applicationContextType;
}
}

throw new JsonMappingException(
MessageFormat.format(
"Configuration Error: appContextType must be either \"{0}\" or \"{1}\"",
SpringConfiguration.ApplicationContextType.WEB_APPLICATION_CONTEXT.getName(),
SpringConfiguration.ApplicationContextType.APPLICATION_CONTEXT.getName()
)
);

}

}
Loading