Git Product home page Git Product logo

dropwizard-spring's Introduction

dropwizard-spring

dropwizard-spring provides a clean, easy way to integrate the spring framework with dropwizard-based web service projects.

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.

Maven Configuration

Maven Dependency

Dropwizard 0.7.0:

<dependency>
	<groupId>com.hmsonline</groupId>
	<artifactId>dropwizard-spring</artifactId>
	<version>0.6.0</version>
</dependency>

Dropwizard 0.6.2 (dropwizard-0.6.2 branch of this repository):

<dependency>
	<groupId>com.hmsonline</groupId>
	<artifactId>dropwizard-spring</artifactId>
	<version>0.5.1</version>
</dependency>

Maven Shade Plugin Configuration

This is required to have maven build a "fat," executable jar file.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<createDependencyReducedPom>true</createDependencyReducedPom>
		<filters>
			<filter>
				<artifact>*</artifact>
				<excludes>
					<exclude>META-INF/*.SF</exclude>
					<exclude>META-INF/*.RSA</exclude>
					<exclude>META-INF/*.INF</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.hmsonline.dropwizard.spring.SpringService</mainClass>
					</transformer>
					<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>
			</configuration>
		</execution>
	</executions>
</plugin>

Sample YAML Configuration

# dropwizard-spring sample service configuration

# High level information that describes the application. This can be pulled into auto documentation tools like Swagger.
apiMetadata:
    rootServerName: www.example.com
    title: My Example Application
    description: Configure and use the application via this API
    contact: [email protected]
    license: License type here
    licenseUrl: http://www.example.com/license
    termsOfServiceUrl: http://www.example.com/tos

spring:

    # Spring Context Type (Required)
    # either "web" or "app"
    appContextType: web

    # Resource Location Type (Required)
    # either "file" or "classpath"
    configLocationsType: file

    # Spring Config Locations (Required)
    # The location of one or more beans.xml files
    configLocations:
       - conf/dropwizard-beans.xml

    # Servlet Filter
    # List of FilterConfiguration
    filters:
        springSecurityFilterChain:
            clazz: org.springframework.web.filter.DelegatingFilterProxy
            url: "/*"

    # List of ServletConfiguration
    servlets:
        cxf:
            clazz: org.apache.cxf.transport.servlet.CXFServlet 
            url: "/ws/soap/*"

    # JAX-RS Resources (Required if you want your service to do anything)
    # one or more spring beans that are JAX-RS resources
    resources:
       - myRestResourceBean
       - anotherRestResourceBean

    # DW Health Checks (Optional, but recommended)
    # list of health check beans (must extend com.yammer.metrics.core.HealthCheck)
    healthChecks:
       - myHealthCheckBean

    # Jersey Providers (Optional)
    # list of Jersey Provider beans (ExceptionMappers, etc.)
    jerseyProviders:
        - myExceptionMapper

    # DW Managed Objects (Optional)
    # one or more instance of com.yammer.dropwizard.lifecycle.Managed
    managed:
        - myManagedObjectBean

    # Jersey Life Cycles (Optional)
    # one or more life cycle instances
    lifeCycles:
        - myLifeCycleBean
    
    # DW Tasks (Optional)
    # one or more instance of com.yammer.dropwizard.tasks.Task
    tasks:
        - myDropwizardTaskBean
   

    # Enabled/Disabled Jersey Features (Optional)
    # list of Jersey features to enable/disable
    enabledJerseyFeatures:
        - com.sun.jersey.config.feature.CanonicalizeURIPath

    disabledJerseyFeatures:
       - com.sun.jersey.config.feature.DisableWADL

Accessing Dropwizard Configuration Values from Spring

In some cases it may be necessary for a spring-configured component to access values in the dropwizard configuration or the com.yammer.dropwizard.config.Environment instance. For example, referencing the port on which the service is configured to run.

To allow this, at startup the dropwizard-spring service injects a special "dropwizard" bean into the application context that contains both the dropwizard environment and configuration objects.

The spring beans.xml example below illustrates how to reference values from the dropwizard yaml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jms="http://www.springframework.org/schema/jms" xmlns:amq="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
                           http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

	<bean id="helloResource" class="com.hmsonline.dropwizard.spring.sample.HelloResource">
		<property name="port">
		<util:property-path path="dropwizard.configuration.httpConfiguration.port"></util:property-path>
	</property>
	</bean>
</beans>

Using an IDE to run your dropwizard-spring service

If you'd like to run or debug your dropwizard-spring service with an IDE (e.g. IntelliJ IDEA) follow these simple steps:

  • Click Run -> Edit Configurations
  • Add a new configuration (Application)
  • Give it a name like Dropwizard Spring
  • For Main Class enter com.hmsonline.dropwizard.spring.SpringService
  • For Program arguments enter server conf/config.yaml
  • For Working directory enter the root directory of your webservice (the location of your pom.xml)
  • For Use classpath of module select your webservice application
  • Remove Make from Before launch
  • Click OK
  • Now, in the menu you can click Run -> Run 'Dropwizard Spring' or Run -> Debug 'Dropwizard Spring'

dropwizard-spring's People

Contributors

bflad avatar boneill42 avatar deanpoulin avatar hellomadhur avatar liorhar avatar mdbouchard avatar pabrahamsson avatar ptgoetz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dropwizard-spring's Issues

Issue with DW managed objects

Hi there,

I'm having a bit of a hard time getting managed objects up and running, whenever I try adding managed beans I get something like

Exception in thread "main" java.lang.IllegalArgumentException: Unknown bean com.schnaub.managed.Foo@3ff0dbfd
    at org.eclipse.jetty.util.component.ContainerLifeCycle.manage(ContainerLifeCycle.java:352)
    at com.hmsonline.dropwizard.spring.SpringService.loadManagedBeans(SpringService.java:165)
    at com.hmsonline.dropwizard.spring.SpringService.run(SpringService.java:60)
    at com.hmsonline.dropwizard.spring.SpringService.run(SpringService.java:30)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:42)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
    at io.dropwizard.cli.Cli.run(Cli.java:70)
    at io.dropwizard.Application.run(Application.java:72)

In the main app I'm on right now all other beans are created just fine.

I've created a super simple demo app here that highlights the issue: https://github.com/JanGorman/ManagedBeanDemo/

Thanks in advance.
Best regards
Jan

Issue parsing yaml when certain dw components included

First off - nice underrated library - thank you for your efforts on it.

In my particular tests, when using 0.3 and defining 'jerseyProviders', the configuration is parsed without throwing an exception (during startup). In 0.4, I see the error:

Exception in thread "main" while parsing a block mapping
in "", line 2, column 4:
appContextType: app
^
expected , but found BlockMappingStart
in "", line 34, column 5:
jerseyProviders:
^

If I don't include jerseyProviders, the configuration parses successfully in 0.4. I haven't yet tried defining other jersey components (beyond resources).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.