Dropwizard Spring - the perfect Java REST server stack


Published on

An introduction to Dropwizard, as well as examples of how to integrate it with Spring DI and Spring Security. Prepared for a presentation at the Houston Java User Group. Code samples at : https://github.com/jacek99/dropwizard-spring-di-security-onejar-example

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Dropwizard Spring - the perfect Java REST server stack

  1. 1. Dropwizard & Spring The perfect Java REST stack Jacek Furmankiewicz Architect @ PROS, Inc. 1
  2. 2. 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
  3. 3. 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. 4. 4 Dropwizard Dropwizard What is it and why is it so exciting? 4
  5. 5. Dropwizard Created by Coda Hale @ Yammer http://dropwizard.io 5
  6. 6. 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
  7. 7. 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
  8. 8. Dropwizard Anatomy of a Dropwizard application Jetty Jersey YOUR CODE (business logic, DAO, Spring, JPA, etc.) 8
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 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. 13. 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
  14. 14. Dropwizard and Spring Dropwizard and Spring DI Adding dependency injection 14
  15. 15. 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
  16. 16. 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
  17. 17. Dropwizard and Spring Dropwizard and Spring Security Adding enterprise-grade security 17
  18. 18. 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
  19. 19. Dropwizard Dropwizard user comments Words from people who use it every day 19
  20. 20. 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
  21. 21. Dropwizard Q&A PROS Interested in Big Data and real-time challenges? We're hiring! http://www.pros.com/company/working-pros/ 21
  22. 22. 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