REST made simple with Java

1,016 views
882 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,016
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

REST made simple with Java

  1. 1. REST made simple with Java SDC 2008
  2. 2. Niklas Gustavsson niklas@protocol7.com http://protocol7.com http://twitter.com/protocol7
  3. 3. REST made simple with Java
  4. 4. REST?
  5. 5. HTTP 1.1
  6. 6. Some HTTP history
  7. 7. Some HTTP history
  8. 8. HTTP done right
  9. 9. Principles
  10. 10. Principles Everything is a resource
  11. 11. Principles A resource has an identifier http://example.com/customers/1453
  12. 12. Principles We transfer representations
  13. 13. Principles All resources expose a uniform interface GET, POST, PUT, DELETE
  14. 14. Principles Hypermedia as the engine of application state
  15. 15. Principles Client-server, Stateless, Cacheable, Layered
  16. 16. Why?
  17. 17. Why? It's easy! Well, it's not
  18. 18. Why? It's what the cool kids use
  19. 19. Why? Web has been successful, copy!
  20. 20. Why? It's what others use
  21. 21. Why? Interoperability on the right level
  22. 22. Frameworks, yeay!
  23. 23. JAX-RS (aka JSR-311)
  24. 24. Jersey http://jersey.dev.java.net Restlets http://www.restlet.org RESTeasy http://www.jboss.org/resteasy CXF http://cxf.apache.org
  25. 25. POJO based Annotation heavy
  26. 26. Resources
  27. 27. Code! Show me the code!
  28. 28. public class TimeReportService { private TimeReportDao reportDao; public TimeReport getReport(String username) { return reportDao.forUser(username); } public void saveReport(TimeReport report) { reportDao.update(report); } ... }
  29. 29. Request mapping
  30. 30. @Path(quot;reportquot;) public class TimeReportService { private TimeReportDao reportDao; @GET public TimeReport getReport(String username) { return reportDao.forUser(username); } @PUT public void saveReport(TimeReport report) { reportDao.update(report); } ... }
  31. 31. public class TimeReportApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<Class<?>>(); resources.add(TimeReportService.class); return resources; } }
  32. 32. Path parameters and contexts
  33. 33. @Path(quot;{username}/reportquot;) public class TimeReportService { @GET public TimeReport getReport(@PathParam(quot;usernamequot;) String username) { return reportDao.forUser(username); } ... } http://example.com/niklas/report
  34. 34. @GET public TimeReport getReport(@Context SecurityContext sc) { if(sc.isSecure()) { Principal user = sc.getUserPrincipal(); return reportDao.forUser(user.getName()); } else { ... throw error, redirect to login } }
  35. 35. @GET public TimeReport getReport(@CookieParam(quot;usernamequot;) String username) { return reportDao.forUser(username); }
  36. 36. Any media type is allowed XML, JSON, text/plain, Binary files, ...
  37. 37. Entity providers MessageBodyReader, MessageBodyWriter
  38. 38. Standard Entity providers
  39. 39. Writing your own Entity provider
  40. 40. BEGIN:VFREEBUSY DTSTART:20090324T080000Z DTEND:20090324T170000Z DTSTAMP:20090316T123136Z UID:ca668b14ddf589ea8650b0b992eb64a656cdbc7e@google.com SUMMARY:SDC2009 END:VFREEBUSY
  41. 41. @Provider @Produces(quot;text/calendarquot;) public class TimeReportICalWriter implements MessageBodyWriter<TimeReport> { public void writeTo(TimeReport t, Class<?> type, Type genericType,Annotation[] annotations,MediaType mediaType,MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) { PrintWriter wr = new PrintWriter(entityStream); ... for(TimeRange range : t.getRanges()) { wr.println(quot;BEGIN:VFREEBUSYquot;); wr.println(quot;DTSTART:quot; + DF.format(range.getStartTime())); wr.println(quot;DTEND:quot; + DF.format(range.getEndTime())); wr.println(quot;SUMMARY:quot; + range.getDescription()); wr.println(quot;END:VFREEBUSYquot;); } ... }
  42. 42. Response, ResponseBuilder
  43. 43. Exception Mapping
  44. 44. Deployment
  45. 45. JAX-RS limitations
  46. 46. JAX-RS limitations Lifecycle support
  47. 47. JAX-RS limitations Weak support for links, caching, method tunneling
  48. 48. JAX-RS limitations Accept based content negotiation only http://example.com/report http://example.com/report.xml http://example.com/report.cal http://example.com/report.json
  49. 49. JAX-RS limitations Limited security support
  50. 50. JAX-RS limitations Leaky abstraction
  51. 51. Try it out! And have a look at the alternatives
  52. 52. Want more?
  53. 53. Questions?
  54. 54. niklas@protocol7.com
  55. 55. Attributions http://www.flickr.com/photos/psd/421186578/ http://www.flickr.com/photos/sineout/2491569707/ http://www.flickr.com/photos/apelad/sets/72157594388426362/ http://www.flickr.com/photos/goopymart/289959670

×