These are the slides from my talk at the Groovy Users of Minnesota group. I gave this talk on Feb 12th 2013 and it describes how we use dropwizard at Bloomhealth.
building web services on the JVM. It is mostly glue around mature java libraries like Jetty, Jersey, Jackson and Guava. “Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality HTTP+JSON web service in the shortest time possible. ”
at all is to provide opinions on what a service should be. It uses fat JARs because I think they work better. It embeds Jetty because I think that works better. It uses Jackson because I think that works better. It uses Jersey because I think that works better. It has a single YAML configuration file because I think that works better. It wraps Logback because I think that works better. WHO CREATED IT?
applications Dropwizard for JSON web services Gradle for builds Swagger for Service Discovery Spock for testing Gatling for Performace/Load Testing Redis for Caching RabbitMQ for messaging
starts jetty. No need for a container. Start the server from the command line by running: p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) t h r o w s E x c e p t i o n { n e w C o n t a c t s S e r v i c e ( ) . r u n ( a r g s ) } j a v a - j a r c o n t a c t _ d r o p w i z a r d / b u i l d / l i b s / c o n t a c t _ d r o p w i z a r d - s h a d o w - 0 . 1 . 0 - S N A P S H O T . j a r s e r v e r s t a r t d e v _ c o n f i g . y m l
tasks and resources. The service class defines all of the abilities of your application. @ O v e r r i d e p u b l i c v o i d i n i t i a l i z e ( B o o t s t r a p b o o t s t r a p ) { b o o t s t r a p . n a m e = ' c o n f i g u r a t i o n _ s e r v i c e ' b o o t s t r a p . a d d B u n d l e m i g r a t i o n s B u n d l e b o o t s t r a p . a d d B u n d l e h i b e r n a t e B u n d l e b o o t s t r a p . a d d B u n d l e ( n e w A s s e t s B u n d l e ( ' / s w a g g e r - u i - 1 . 1 . 0 / ' , ' / s w a g g e r ' ) ) b o o t s t r a p . a d d C o m m a n d ( n e w m i g r a t i o n s C o m m a n d ( ) ) } @ O v e r r i d e p u b l i c v o i d r u n ( C o n t a c t s C o n f i g u r a t i o n c o n f i g u r a t i o n , E n v i r o n m e n t e n v i r o n m e n t ) t h r o w s C l a s s N o t F o u n d E x c e p t i o n { C o n t a c t D A O c o n t a c t D A O = n e w C o n t a c t D A O ( h i b e r n a t e B u n d l e . s e s s i o n F a c t o r y ) e n v i r o n m e n t . a d d R e s o u r c e ( n e w C o n t a c t R e s o u r c e ( c o n t a c t D A O ) ) }
API. Dropwizard uses Jersey for this so these classes are mostly jersey annotations. @ P a t h ( ' / c o n t a c t s ' ) @ P r o d u c e s ( M e d i a T y p e . A P P L I C A T I O N _ J S O N ) c l a s s C o n t a c t R e s o u r c e { p r i v a t e f i n a l C o n t a c t D A O c o n t a c t D A O p u b l i c C o n t a c t R e s o u r c e ( C o n t a c t D A O c o n t a c t D A O ) { t h i s . c o n t a c t D A O = c o n t a c t D A O } @ T i m e d ( n a m e = ' c r e a t e C o n t a c t ' ) @ P O S T @ U n i t O f W o r k p u b l i c C o n t a c t c r e a t e C o n t a c t ( @ V a l i d C o n t a c t c o n t a c t ) { r e t u r n c o n t a c t D A O . s a v e O r U p d a t e ( c o n t a c t ) } }
Jackson. Hibernate Validator lets you specify validation rules. @ T o S t r i n g @ E q u a l s A n d H a s h C o d e c l a s s C o n t a c t { @ J s o n P r o p e r t y L o n g i d @ N o t N u l l @ N o t E m p t y @ J s o n P r o p e r t y S t r i n g f i r s t N a m e @ N o t N u l l @ N o t E m p t y @ J s o n P r o p e r t y S t r i n g l a s t N a m e @ T r a n s i e n t @ J s o n I g n o r e
an administration port (8081). Resources can be annotated with @ T i m e d or @ M e t e r e d , and @ E x c e p t i o n M e t e r e d . Dropwizard augments Jersey to automatically record runtime information about your resource methods.
the infrastructure your service depends on are all running. They are accessible on the administration port. p u b l i c c l a s s M y S Q L H e a l t h C h e c k e x t e n d s H e a l t h C h e c k { p r i v a t e f i n a l S e s s i o n F a c t o r y s e s s i o n F a c t o r y p u b l i c M y S Q L H e a l t h C h e c k ( S e s s i o n F a c t o r y s e s s i o n F a c t o r y ) { s u p e r ( " M y S Q L " ) t h i s . s e s s i o n F a c t o r y = s e s s i o n F a c t o r y } @ O v e r r i d e p r o t e c t e d c o m . y a m m e r . m e t r i c s . c o r e . H e a l t h C h e c k . R e s u l t c h e c k ( ) { i f ( ! s e s s i o n F a c t o r y . c l o s e d ) { r e t u r n h e a l t h y ( ) } e l s e { r e t u r n u n h e a l t h y ( ' S e s s i o n F a c t o r y i s C l o s e d ! ' ) } } }
your service. For example the server starts based on the 'server' command. Migrations run based on the 'db migrate' command. You might add your own command for running functional tests or seeding the database.