REST made simple with Java

download REST made simple with Java

of 59

  • date post

    10-May-2015
  • Category

    Technology

  • view

    822
  • download

    5

Embed Size (px)

Transcript of REST made simple with Java

  • 1.REST made simple with JavaSDC 2008

2. Niklas Gustavsson niklas@protocol7.com 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. Principles 10. Principles Everything is a resource 11. Principles A resource has an identifier http://example.com/customers/1453 12. Principles We transfer representations 13. Principles All resources expose a uniform interface GET, POST, PUT, DELETE 14. Principles Hypermedia as the engine of application state 15. Principles 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, yeay! 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. @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. public class TimeReportApplication extends Application {@Override public Set> getClasses() { Set> resources = new HashSet>();resources.add(TimeReportService.class);return resources; } } 32. Path parameters and contexts 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. @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(quot;usernamequot;) String username) {return reportDao.forUser(username); } 36. Any media type is allowed XML, JSON, text/plain, Binary files, ... 37. Entity providers MessageBodyReader, MessageBodyWriter 38. Standard Entity providers 39. Writing your own Entity provider 40. BEGIN:VFREEBUSY DTSTART:20090324T080000Z DTEND:20090324T170000Z DTSTAMP:20090316T123136Z UID:ca668b14ddf589ea8650b0b992eb64a656cdbc7e@google.com SUMMARY:SDC2009 END:VFREEBUSY 41. @Provider @Produces(quot;text/calendarquot;) public class TimeReportICalWriter implementsMessageBodyWriter { public void writeTo(TimeReport t, Class type, TypegenericType,Annotation[] annotations,MediaTypemediaType,MultivaluedMap 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. Response, ResponseBuilder 43. ExceptionMapping 44. Deployment 45. JAX-RS limitations 46. JAX-RS limitations Lifecycle support 47. JAX-RS limitations Weak support for links, caching, method tunneling 48. JAX-RS limitations Accept based content negotiation onlyhttp://example.com/report http://example.com/report.xml http://example.com/report.cal http://example.com/report.json 49. JAX-RS limitations Limited security support 50. JAX-RS limitations Leaky abstraction 51. Try it out! And have a look at the alternatives 52. Want more? 53. Questions? 54. niklas@protocol7.com 55. Attributionshttp://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