Your SlideShare is downloading. ×
REST made simple with Java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

REST made simple with Java

6,051
views

Published on

Slides from my SDC2009 presentation on JAX-RS.

Slides from my SDC2009 presentation on JAX-RS.

Published in: Technology, Design

1 Comment
12 Likes
Statistics
Notes
No Downloads
Views
Total Views
6,051
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
191
Comments
1
Likes
12
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
  • 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 done right
    • 9.  
    • 10. Principles
    • 11. Principles Everything is a resource
    • 12. Principles A resource has an identifier http://example.com/customers/1453
    • 13. Principles We transfer representations
    • 14. Principles All resources expose a uniform interface GET, POST, PUT, DELETE
    • 15. Principles Hypermedia as the engine of application state
    • 16. Principles Client-server, Stateless, Cacheable, Layered
    • 17. Why?
    • 18. Why? It's easy! Well, it's not
    • 19. Why? It's what the cool kids use
    • 20. Why? Web has been successful, copy!
    • 21. Why? It's what others use
    • 22. Why? Interoperability on the right level
    • 23. Frameworks, yeay!
    • 24. JAX-RS (aka JSR-311)
    • 25. Jersey http://jersey.dev.java.net Restlets http://www.restlet.org RESTeasy http://www.jboss.org/resteasy CXF http://cxf.apache.org
    • 26. POJO based Annotation heavy
    • 27. Resources
    • 28. Code! Show me the code!
    • 29. public class TimeReportService { private TimeReportDao reportDao; public TimeReport getReport(String username) { return reportDao.forUser(username); } public void saveReport(TimeReport report) { reportDao.update(report); } ... }
    • 30. Request mapping
    • 31. @Path("report") 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.  
    • 33. public class TimeReportApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<Class<?>>(); resources.add(TimeReportService.class); return resources; } }
    • 34. Path parameters and contexts
    • 35. @Path(&quot;{username}/report&quot;) public class TimeReportService { @GET public TimeReport getReport(@PathParam(&quot;username&quot;) String username) { return reportDao.forUser(username); } ... } http://example.com/niklas/report
    • 36. @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 } }
    • 37. @GET public TimeReport getReport(@CookieParam(&quot;username&quot;) String username) { return reportDao.forUser(username); }
    • 38.  
    • 39. Any media type is allowed XML, JSON, text/plain, Binary files, ...
    • 40. Entity providers MessageBodyReader, MessageBodyWriter
    • 41. Standard Entity providers
    • 42.  
    • 43. Writing your own Entity provider
    • 44. BEGIN:VFREEBUSY DTSTART:20090324T080000Z DTEND:20090324T170000Z DTSTAMP:20090316T123136Z UID:ca668b14ddf589ea8650b0b992eb64a656cdbc7e@google.com SUMMARY:SDC2009 END:VFREEBUSY
    • 45. @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:VFREEBUSY&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:VFREEBUSY&quot;); } ... }
    • 46. Response, ResponseBuilder
    • 47. Exception Mapping
    • 48. Deployment
    • 49. JAX-RS limitations
    • 50. JAX-RS limitations Lifecycle support
    • 51. JAX-RS limitations Weak support for links, caching, method tunneling
    • 52. 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
    • 53. JAX-RS limitations Limited security support
    • 54. JAX-RS limitations Leaky abstraction
    • 55. Try it out! And have a look at the alternatives
    • 56. Want more?
    • 57. Questions?
    • 58. [email_address]
    • 59. 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