Your SlideShare is downloading. ×
Oredev 2009 JAX-RS
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Oredev 2009 JAX-RS

855
views

Published on

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

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
855
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
40
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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
  • Transcript

    • 1. SDC 2008 REST made simple with Java
    • 2. Niklas Gustavsson [email_address] http://protocol7.com http://twitter.com/protocol7
    • 3. REST made simple with Java
    • 4. REST?
    • 5. HTTP 1.1
    • 6. Some HTTP history
    • 7. Some HTTP history
    • 8. HTTP used right
    • 9. Constraints
    • 10. Constraints Everything is a resource
    • 11. Constraints A resource has an identifier http://example.com/reports/niklas
    • 12. Constraints We transfer representations
    • 13. Constraints All resources expose a uniform interface GET, POST, PUT, DELETE
    • 14. Constraints Hypermedia as the engine of application state
    • 15. Constraints Client-server, Stateless, Cacheable, Layered
    • 16. Why?
    • 17. Why? It's easy! Well, it's not
    • 18. Why? It's what the cool kids use
    • 19. Why? Web has been successful, copy!
    • 20. Why? It's what others use
    • 21. Why? Interoperability on the right level
    • 22. Frameworks!
    • 23. JAX-RS (aka JSR-311)
    • 24. Jersey http://jersey.dev.java.net Restlets http://www.restlet.org RESTeasy http://www.jboss.org/resteasy CXF http://cxf.apache.org
    • 25. POJO based Annotation heavy
    • 26. Resources
    • 27. Code! Show me the code!
    • 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. 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); } ... }
    • 32. Parameters
    • 33. @Path("reports/ {username} ") public class TimeReportService { @GET public TimeReport getReport(@PathParam("username") String username) { return reportDao.forUser(username); } ... } http://example.com/report/ niklas
    • 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. @GET public TimeReport getReport(@CookieParam("username") String username) { return reportDao.forUser(username); }
    • 36. Any media type is allowed XML, JSON, text/plain, binary, ...
    • 37. Entity providers MessageBodyReader, MessageBodyWriter
    • 38.  
    • 39. Standard Entity providers
    • 40. Writing your own Entity provider
    • 41. BEGIN:VEVENT DTSTART:20091102T080000Z DTEND:20091106T170000Z SUMMARY:OREDEV END:VEVENT
    • 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;); } ... }
    • 43. Request Response ResponseBuilder
    • 44. Exception Mapping
    • 45. Application
    • 46.  
    • 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 ; } }
    • 48. Deployment
    • 49. JAX-RS limitations
    • 50. JAX-RS limitations Lifecycle support
    • 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
    • 52. JAX-RS limitations Limited security support
    • 53. JAX-RS limitations Leaky abstraction
    • 54. JAX-RS limitations Weak support for links
    • 55. Try it out! And have a look at the alternatives
    • 56. Want more? http://github.com/protocol7/jax-rs-demo
    • 57. Want more?
    • 58. Questions?
    • 59. [email_address]
    • 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