Introduction to JAX-RS

6,939 views
6,794 views

Published on

An introduction to RESTful web services in Java using JAX-RS

Published in: Technology, News & Politics
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,939
On SlideShare
0
From Embeds
0
Number of Embeds
551
Actions
Shares
0
Downloads
130
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Introduction to JAX-RS

  1. 1. ______/_____/____/_______/________________/________/___ _____///////____/_///___/_______________///////_____/////////_ _________/______/////////___///________________/_____/____//______///__ _________/_____/_______/_____//_______/__/______////_________ _________/_____/______/______////////___//////_________////______ _________/_____/////////______/________________/____//___________////___ __/___/_____/_______/____////_______________/_____//___/______//__ _//_____/_______/__/___///_____________/______//_///___ __/////////_______///________///__///_______///______________///________///____///////////_____ ____/_____/_____/_____/_ __///////___///////////____/////////__///////////__ _/_____/___/______________//______///_________/_______ _/____/_______////________________/_______ _//////____///////___________////_____________/_______ _/____//___/_____________________////__________/_______ _/_____//__/______________/______//_________/_______ _/______//_/_///__________/_______ _///________///__///////////////____///////////____________///________ with 2009-09-15 Andreas Bjärlestam
  2. 2. <ul><ul><li>Andreas Bjärlestam </li></ul></ul><ul><ul><li>mailto:andreas@bjarlestam.com </li></ul></ul><ul><ul><li>http://andreas.bjarlestam.com </li></ul></ul><ul><ul><li>http://twitter.com/bjarlestam </li></ul></ul>
  3. 3. REST? Attribution: http://www.flickr.com/photos/noahbulgaria/270090287
  4. 4. Pragmatic explanation <ul><li>Use HTTP the way it was designed to be used </li></ul><ul><li>Take advantage of the good things in HTTP </li></ul><ul><li>By following a set of constraints </li></ul>
  5. 5. Constraints in REST <ul><li>Give everything its own URI </li></ul><ul><li>Communicate with a standard set of methods </li></ul><ul><li>Communicate with Media Types </li></ul><ul><li>Link your resources together </li></ul><ul><li>Avoid session state </li></ul><ul><li>Support caching </li></ul>* This is a very simplified version of the real REST constraints, for the real stuff read Roy Fieldings dissertation
  6. 6. JAX-RS (JSR 311) <ul><li>Java API for building RESTful services </li></ul>
  7. 7. JAX-RS <ul><li>Based on POJOs with Annotations </li></ul>
  8. 8. JAX-RS <ul><li>Makes the developer focus on URLs, HTTP methods and Media Types. </li></ul>
  9. 9. Lets code…
  10. 10. @Path <ul><li>@Path( &quot;location&quot; ) </li></ul><ul><li>public class LocationResouce { </li></ul><ul><li>} </li></ul>
  11. 11. @GET, @PUT, @POST <ul><li>@Path( &quot;location&quot; ) </li></ul><ul><li>public class LocationResouce { </li></ul><ul><li>@GET </li></ul><ul><li>@Produces( &quot;application/xml&quot; ) </li></ul><ul><li>public String getLocation() { </li></ul><ul><li>return &quot;<location>I’m in Sweden</location>&quot; ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 12. curl
  13. 13. @PathParam, @QueryParam <ul><li>@Path( &quot;location&quot; ) </li></ul><ul><li>public class LocationResouce { </li></ul><ul><li>@GET </li></ul><ul><li>@Path( &quot;{user}&quot; ) </li></ul><ul><li>@Produces( &quot;application/xml&quot; ) </li></ul><ul><li>public String getLocation(@PathParam( &quot;user&quot; ) String user) { </li></ul><ul><li>return &quot;<location>” + user + &quot; is in Sweden</location>&quot; ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  14. 14. JAXB <ul><li>@Path( &quot;location&quot; ) </li></ul><ul><li>public class LocationResouce { </li></ul><ul><li>@GET </li></ul><ul><li>@Path( &quot;{user}&quot; ) </li></ul><ul><li>@Produces( &quot;application/xml&quot; ) </li></ul><ul><li>public Location getLocation(@PathParam( &quot;user&quot; ) String user) { </li></ul><ul><li>return new Location( &quot;59.3&quot; , &quot;18&quot; ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  15. 15. JAXB <ul><li>@XmlRootElement </li></ul><ul><li>public class Location { </li></ul><ul><li>public String lattitude; </li></ul><ul><li>public String longitude; </li></ul><ul><li>public Date timestamp; </li></ul><ul><li>. . . </li></ul><ul><li>} </li></ul>
  16. 17. @Produces and Media Types <ul><li>@Path( &quot;location&quot; ) </li></ul><ul><li>public class LocationResouce { </li></ul><ul><li>@GET </li></ul><ul><li>@Path( &quot;{user}&quot; ) </li></ul><ul><li>@Produces({ &quot;application/xml&quot;, &quot;application/json&quot; }) </li></ul><ul><li>public Location getLocation(@PathParam( &quot;user&quot; ) String user) { </li></ul><ul><li>return new Location( &quot;59.3&quot; , &quot;18&quot; ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 19. @Context, UriInfo and UriBuilder <ul><li>@Path( &quot;location&quot; ) </li></ul><ul><li>public class LocationResouce { </li></ul><ul><li>@Context UriInfo uriInfo; </li></ul><ul><li>. . . </li></ul><ul><li>@POST </li></ul><ul><li>@Consumes( &quot;application/x-www-form-urlencoded&quot; ) </li></ul><ul><li>public Response saveLocation(@FormParam( &quot;user&quot; ) String user) { </li></ul><ul><li>locations.put (user, new Location( &quot;59.3&quot; , &quot;18&quot; )); </li></ul><ul><li>UriBuilder uriBuilder = uriInfo .getAbsolutePathBuilder(); </li></ul><ul><li>URI userUri = uriBuilder.path(user).build(); </li></ul><ul><li>return Response.created(userUri).build(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 21. Cache Control <ul><li>@GET </li></ul><ul><li>@Path( &quot;{user}&quot; ) </li></ul><ul><li>@Produces({ &quot;application/xml&quot;, &quot;application/json&quot; }) </li></ul><ul><li>public Response getLocation(@PathParam( &quot;user&quot; ) String user) { </li></ul><ul><li>Location l = locations .get(user); </li></ul><ul><li>CacheControl cc = new CacheControl(); </li></ul><ul><li>cc.setMaxAge(500); </li></ul><ul><li>Response.ResponseBuilder builder = Response.ok(l); </li></ul><ul><li>builder.cacheControl(cc); </li></ul><ul><li>return builder.build() ; </li></ul><ul><li>} </li></ul>
  19. 23. For the interested
  20. 24. For the interested <ul><li>Roy Fieldings PhD dissertation, see ch 5 </li></ul><ul><ul><li>http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm </li></ul></ul><ul><li>Roy Fieldings blog </li></ul><ul><ul><li>http://roy.gbiv.com/untangled/ </li></ul></ul><ul><li>Mark Hadleys blog </li></ul><ul><ul><li>http://weblogs.java.net/blog/mhadley/ </li></ul></ul><ul><li>Mark Nottinghams blog </li></ul><ul><ul><li>http://www.mnot.net </li></ul></ul><ul><li>Stefan Tilkovs blog </li></ul><ul><ul><li>http://www.innoq.com/blog/st/ </li></ul></ul>
  21. 25. Extra slides
  22. 26. JAX-RS <ul><li>JSR 311 </li></ul><ul><li>Version 1.0 was finalized in september 2008 </li></ul><ul><li>Version 1.1 is planned to be part of JEE6 </li></ul>
  23. 27. JAX-RS <ul><li>Several implementations available </li></ul><ul><li>Sun Jersey </li></ul><ul><li>JBoss RestEasy </li></ul><ul><li>Restlets </li></ul><ul><li>Apache CXF </li></ul><ul><li>Triaxrs </li></ul><ul><li>Apache WINK </li></ul>
  24. 28. Serendipity <ul><li>By giving everything a URL, sticking to a Uniform Interface and linking your resources you make the chances of reuse in unexpected ways higher </li></ul>
  25. 29. Statelessness <ul><li>Better scaling </li></ul><ul><ul><li>No session storage on server </li></ul></ul><ul><ul><li>Any server in a cluster can handle any request </li></ul></ul><ul><ul><li>The resources can safely be cached </li></ul></ul><ul><li>More reliable </li></ul><ul><ul><li>A client can safely re-send a request </li></ul></ul><ul><li>Better resource reuse </li></ul><ul><ul><li>The resources can safely be linked to </li></ul></ul>
  26. 30. Caching
  27. 31. Client Proxy Cache Client Client Server Proxy Cache Client Reverse Proxy Cache Client Cache Client Cache Client Cache Client Cache
  28. 32. “ The best requests are those that not even reach me.” - Anonymous overloaded Server
  29. 33. 50 requests/second = 3000 requests/minute setting max-age=60 (seconds) can then save you 2999 requests
  30. 34. Caching <ul><li>GET can be cached while POST can't (safely) </li></ul><ul><li>Specify max-age </li></ul><ul><ul><li>Use max-age cache control header </li></ul></ul><ul><li>Split data according to freshness requirement </li></ul><ul><li>Support conditional GET </li></ul><ul><ul><li>Get if modified since </li></ul></ul><ul><ul><li>E-tags </li></ul></ul>

×