Dropwizard & Spring
The perfect Java REST stack

Jacek Furmankiewicz
Architect @ PROS, Inc.
1
Welcome to Houston TechFest
Thank you for being a part of the
7th Annual Houston TechFest!
Please turn off all electronic devices or set them to vibrate.
If you must take a phone call, please do so in the lobby so
as not to disturb others.
Thanks to our Diamond Sponsors:

2
Information
Speaker presentation slides will be available at www.houstontechfest.org within a
week
Don’t forget to complete the Bingo card to be eligible for door prizes

3
4
Dropwizard

Dropwizard

What is it and why is it so exciting?

4
Dropwizard

Created by Coda Hale @ Yammer
http://dropwizard.io

5
Dropwizard

Best-of-breed Java libraries integrated into one
embedded application package
●

Embedded Jetty (no WAR, no external servlet container)

●

JAX-RS (Jersey) for REST APIs

●

JSON (Jackson)

●

Logging (Logback and SLF4J)

●

Hibernate Validators

●

Metrics (application SLA and performance)

6
Dropwizard

Embedded Jetty
●

Restart your code in seconds. Run your code from the IDE.

●

No WAR to recompile (ever!)

●

No WAR to redeploy (ever!)

●

No servlet container (Tomcat, Websphere, Weblogic, etc) to install

●

●

●

Debug from your IDE (you have a main()), no need to attach to a separate
process
No need to share heap and GC issues with other apps running in the same
servlet container
Total process isolation (one misbehaving WAR (e.g. OutOfMemoryException)
cannot bring down the entire server process)

7
Dropwizard

Anatomy of a Dropwizard application

Jetty

Jersey

YOUR CODE
(business logic, DAO,
Spring, JPA, etc.)

8
Dropwizard

Multiple apps on the same server
pid 1843
Jetty

Jersey

APP 1

Own JVM
4 GB heap

Jersey

APP 2

Own JVM
4 GB heap

Jersey

APP 3

Own JVM
4 GB heap

pid 1407
Jetty
pid 1976
Jetty

9
Dropwizard

Operations-friendly and ready for deployment
●

Opens 2 HTTP ports: one for public APIs (i.e. your REST services), one for
admin APIs (e.g. run GC, refresh internal caches, etc)

●

Admin port can be closed off on the firewall and inaccessible to outside world

●

Health Check APIs to allow easy monitoring from external tools like Nagios

●

@Timed annotation on any single REST API allows to track its SLA using Metrics
library

10
Dropwizard

Ease of deployment
●

●

Dropwizard apps can be easily compiled into a single JAR with all dependencies
(e.g. using Maven Shade or Gradle Shadow plugins)
Your entire app consists of two files:
YAML config file
JAR application file

●

Trivial to run from command line:
java -server myapp.jar server myapp.yml

11
Dropwizard

Ease of deployment – part 2
●

Can be wrapped in an RPM or DEB to install on Linux clusters

●

Can be registered as a Linux daemon, e.g.
sudo start myapp
sudo stop myapp

12
Dropwizard

Embedding HTML resources
●

●

●

Dropwizard provides the concept of AssetBundle, which allows to bundle
HTML/JS/CSS assets directly in the app (via your src/main/resources folder)
Perfect for creating a modern HTML5 Javascript UI (Backbone.js, Ember.js,
Angular.js, etc) powered by rock-solid super fast Java REST APIs – the best of
both worlds
In 2013, you should not be using any Java (or .Net or Python or Ruby) serverside web frameworks.
HTML5 and Javascript have won and are the future of web development.
Java is best at providing REST APIs to power them with top reliability and
performance.

13
Dropwizard and Spring

Dropwizard and Spring DI
Adding dependency injection

14
Dropwizard and Spring

Integrating Spring DI
●

●

●

●

Start up parent Spring context (AnnotationConfigWebApplicationContext)
Inject Dropwizard Configuration object into it (so that it is available as a Spring
bean)
Start a child context (with the parent in it), also of type
AnnotationConfigWebApplicationContext
Extract all the Spring beans that Dropwizard cares about from the context and
register them within Dropwizard:
–
–

●

JAX-RS @Path resources and @Provider classes
Dropwizard Task and HealthCheck objects

Link Spring with the embedded Jetty instance, so that it behaves just as if it were
running in a regular servlet container

15
Dropwizard and Spring

Integrating Spring DI – part 2
Sample app with a full example:
https://github.com/jacek99/dropwizard-spring-di-security-onejar-example

In particular this class has all the low-level details:
MyAppService.java

16
Dropwizard and Spring

Dropwizard and Spring
Security
Adding enterprise-grade security

17
Dropwizard and Spring Security
●

@ImportResource Spring Security XML config file into your root Spring
@Configuration class

●

Activate Spring Security filter

●

Provides both HTTP and method level (@Secured) support

●

Present in github sample app covered previously

18
Dropwizard

Dropwizard
user comments
Words from people who use it
every day
19
Dropwizard
●

●

Dropwizard is a godsend for all those of us who just need to get stuff DONE.
Dropwizard changed the rules of the game completely. We have dramatically
changed the way of working and increased the speed of whole development
process. Dropwizard is being considered as a Restful Application Framework but
this is totally unfair and limiting its potential. We have just added a few classes
without forking it to operate Spring Framework and it is working wonderfully. That
is to say, Dropwizard can be also considered as a "embedded application
development framework" and we are using in this way

20
Dropwizard

Q&A
PROS
Interested in Big Data and real-time
challenges?
We're hiring!
http://www.pros.com/company/working-pros/

21
Please Leave Feedback During Q&A
If you leave
session feedback
and provide
contact
information, you
will be qualified for
a prize
Scan the QR code
to the right or go
to bit.ly/htf130514
22

Dropwizard Spring - the perfect Java REST server stack

  • 1.
    Dropwizard & Spring Theperfect Java REST stack Jacek Furmankiewicz Architect @ PROS, Inc. 1
  • 2.
    Welcome to HoustonTechFest Thank you for being a part of the 7th Annual Houston TechFest! Please turn off all electronic devices or set them to vibrate. If you must take a phone call, please do so in the lobby so as not to disturb others. Thanks to our Diamond Sponsors: 2
  • 3.
    Information Speaker presentation slideswill be available at www.houstontechfest.org within a week Don’t forget to complete the Bingo card to be eligible for door prizes 3
  • 4.
    4 Dropwizard Dropwizard What is itand why is it so exciting? 4
  • 5.
    Dropwizard Created by CodaHale @ Yammer http://dropwizard.io 5
  • 6.
    Dropwizard Best-of-breed Java librariesintegrated into one embedded application package ● Embedded Jetty (no WAR, no external servlet container) ● JAX-RS (Jersey) for REST APIs ● JSON (Jackson) ● Logging (Logback and SLF4J) ● Hibernate Validators ● Metrics (application SLA and performance) 6
  • 7.
    Dropwizard Embedded Jetty ● Restart yourcode in seconds. Run your code from the IDE. ● No WAR to recompile (ever!) ● No WAR to redeploy (ever!) ● No servlet container (Tomcat, Websphere, Weblogic, etc) to install ● ● ● Debug from your IDE (you have a main()), no need to attach to a separate process No need to share heap and GC issues with other apps running in the same servlet container Total process isolation (one misbehaving WAR (e.g. OutOfMemoryException) cannot bring down the entire server process) 7
  • 8.
    Dropwizard Anatomy of aDropwizard application Jetty Jersey YOUR CODE (business logic, DAO, Spring, JPA, etc.) 8
  • 9.
    Dropwizard Multiple apps onthe same server pid 1843 Jetty Jersey APP 1 Own JVM 4 GB heap Jersey APP 2 Own JVM 4 GB heap Jersey APP 3 Own JVM 4 GB heap pid 1407 Jetty pid 1976 Jetty 9
  • 10.
    Dropwizard Operations-friendly and readyfor deployment ● Opens 2 HTTP ports: one for public APIs (i.e. your REST services), one for admin APIs (e.g. run GC, refresh internal caches, etc) ● Admin port can be closed off on the firewall and inaccessible to outside world ● Health Check APIs to allow easy monitoring from external tools like Nagios ● @Timed annotation on any single REST API allows to track its SLA using Metrics library 10
  • 11.
    Dropwizard Ease of deployment ● ● Dropwizardapps can be easily compiled into a single JAR with all dependencies (e.g. using Maven Shade or Gradle Shadow plugins) Your entire app consists of two files: YAML config file JAR application file ● Trivial to run from command line: java -server myapp.jar server myapp.yml 11
  • 12.
    Dropwizard Ease of deployment– part 2 ● Can be wrapped in an RPM or DEB to install on Linux clusters ● Can be registered as a Linux daemon, e.g. sudo start myapp sudo stop myapp 12
  • 13.
    Dropwizard Embedding HTML resources ● ● ● Dropwizardprovides the concept of AssetBundle, which allows to bundle HTML/JS/CSS assets directly in the app (via your src/main/resources folder) Perfect for creating a modern HTML5 Javascript UI (Backbone.js, Ember.js, Angular.js, etc) powered by rock-solid super fast Java REST APIs – the best of both worlds In 2013, you should not be using any Java (or .Net or Python or Ruby) serverside web frameworks. HTML5 and Javascript have won and are the future of web development. Java is best at providing REST APIs to power them with top reliability and performance. 13
  • 14.
    Dropwizard and Spring Dropwizardand Spring DI Adding dependency injection 14
  • 15.
    Dropwizard and Spring IntegratingSpring DI ● ● ● ● Start up parent Spring context (AnnotationConfigWebApplicationContext) Inject Dropwizard Configuration object into it (so that it is available as a Spring bean) Start a child context (with the parent in it), also of type AnnotationConfigWebApplicationContext Extract all the Spring beans that Dropwizard cares about from the context and register them within Dropwizard: – – ● JAX-RS @Path resources and @Provider classes Dropwizard Task and HealthCheck objects Link Spring with the embedded Jetty instance, so that it behaves just as if it were running in a regular servlet container 15
  • 16.
    Dropwizard and Spring IntegratingSpring DI – part 2 Sample app with a full example: https://github.com/jacek99/dropwizard-spring-di-security-onejar-example In particular this class has all the low-level details: MyAppService.java 16
  • 17.
    Dropwizard and Spring Dropwizardand Spring Security Adding enterprise-grade security 17
  • 18.
    Dropwizard and SpringSecurity ● @ImportResource Spring Security XML config file into your root Spring @Configuration class ● Activate Spring Security filter ● Provides both HTTP and method level (@Secured) support ● Present in github sample app covered previously 18
  • 19.
    Dropwizard Dropwizard user comments Words frompeople who use it every day 19
  • 20.
    Dropwizard ● ● Dropwizard is agodsend for all those of us who just need to get stuff DONE. Dropwizard changed the rules of the game completely. We have dramatically changed the way of working and increased the speed of whole development process. Dropwizard is being considered as a Restful Application Framework but this is totally unfair and limiting its potential. We have just added a few classes without forking it to operate Spring Framework and it is working wonderfully. That is to say, Dropwizard can be also considered as a "embedded application development framework" and we are using in this way 20
  • 21.
    Dropwizard Q&A PROS Interested in BigData and real-time challenges? We're hiring! http://www.pros.com/company/working-pros/ 21
  • 22.
    Please Leave FeedbackDuring Q&A If you leave session feedback and provide contact information, you will be qualified for a prize Scan the QR code to the right or go to bit.ly/htf130514 22