Oredev 2009 JAX-RS

1,152 views

Published on

Introduction to JAX-RS that I gave at Øredev 2009

Published in: Technology, Design
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,152
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
42
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • From Göteborg Callista
  • Oredev 2009 JAX-RS

    1. 1. SDC 2008 REST made simple with Java
    2. 2. Niklas Gustavsson [email_address] 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 used right
    9. 9. Constraints
    10. 10. Constraints Everything is a resource
    11. 11. Constraints A resource has an identifier http://example.com/reports/niklas
    12. 12. Constraints We transfer representations
    13. 13. Constraints All resources expose a uniform interface GET, POST, PUT, DELETE
    14. 14. Constraints Hypermedia as the engine of application state
    15. 15. Constraints 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!
    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. 31. @Path("reports") 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. 32. Parameters
    32. 33. @Path("reports/ {username} ") public class TimeReportService { @GET public TimeReport getReport(@PathParam("username") String username) { return reportDao.forUser(username); } ... } http://example.com/report/ niklas
    33. 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 } }
    34. 35. @GET public TimeReport getReport(@CookieParam("username") String username) { return reportDao.forUser(username); }
    35. 36. Any media type is allowed XML, JSON, text/plain, binary, ...
    36. 37. Entity providers MessageBodyReader, MessageBodyWriter
    37. 39. Standard Entity providers
    38. 40. Writing your own Entity provider
    39. 41. BEGIN:VEVENT DTSTART:20091102T080000Z DTEND:20091106T170000Z SUMMARY:OREDEV END:VEVENT
    40. 42. @Provider @Produces(&quot;text/calendar&quot;) 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:VEVENT&quot;); 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:VEVENT&quot;); } ... }
    41. 43. Request Response ResponseBuilder
    42. 44. Exception Mapping
    43. 45. Application
    44. 47. public class TimeReportApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<Class<?>>(); classes .add(TimeReportService.class); classes .add(TimeReportICalWriter.class); return classes ; } }
    45. 48. Deployment
    46. 49. JAX-RS limitations
    47. 50. JAX-RS limitations Lifecycle support
    48. 51. JAX-RS limitations Conneg only http://example.com/report http://example.com/report.xml http://example.com/report.cal http://example.com/report.json
    49. 52. JAX-RS limitations Limited security support
    50. 53. JAX-RS limitations Leaky abstraction
    51. 54. JAX-RS limitations Weak support for links
    52. 55. Try it out! And have a look at the alternatives
    53. 56. Want more? http://github.com/protocol7/jax-rs-demo
    54. 57. Want more?
    55. 58. Questions?
    56. 59. [email_address]
    57. 60. 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

    ×