SDC  2008 REST  made simple with Java
Niklas Gustavsson [email_address] http://protocol7.com http://twitter.com/protocol7
REST made simple with Java
REST?
HTTP 1.1
Some HTTP history
Some HTTP history
HTTP done right
 
Principles
Principles Everything is a resource
Principles A resource has an identifier http://example.com/customers/1453
Principles We transfer representations
Principles All resources expose a uniform interface  GET, POST, PUT, DELETE
Principles Hypermedia as the engine of application state
Principles Client-server, Stateless, Cacheable, Layered
Why?
Why? It's easy! Well, it's not
Why? It's what the cool kids use
Why? Web has been successful, copy!
Why? It's what others use
Why? Interoperability on the right level
Frameworks, yeay!
JAX-RS (aka JSR-311)
Jersey  http://jersey.dev.java.net Restlets  http://www.restlet.org RESTeasy  http://www.jboss.org/resteasy CXF  http://cxf.apache.org
POJO based Annotation heavy
Resources
Code! Show me the code!
public class TimeReportService { private TimeReportDao reportDao; public TimeReport getReport(String username) { return reportDao.forUser(username); } public void saveReport(TimeReport report) { reportDao.update(report); }   ... }
Request mapping
@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); }   ... }
 
public class TimeReportApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<Class<?>>(); resources.add(TimeReportService.class); return resources; } }
Path parameters and contexts
@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
@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  }  }
@GET public TimeReport getReport(@CookieParam(&quot;username&quot;)  String username) { return reportDao.forUser(username); }
 
Any media type is allowed XML, JSON, text/plain, Binary files, ...
Entity providers MessageBodyReader, MessageBodyWriter
Standard Entity providers
 
Writing your own Entity provider
BEGIN:VFREEBUSY DTSTART:20090324T080000Z DTEND:20090324T170000Z DTSTAMP:20090316T123136Z UID:ca668b14ddf589ea8650b0b992eb64a656cdbc7e@google.com SUMMARY:SDC2009 END:VFREEBUSY
@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;); } ... }
Response, ResponseBuilder
Exception  Mapping
Deployment
JAX-RS limitations
JAX-RS limitations Lifecycle support
JAX-RS limitations Weak support for links, caching, method tunneling
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
JAX-RS limitations Limited security support
JAX-RS limitations Leaky abstraction
Try it out! And have a look at the alternatives
Want more?
Questions?
[email_address]
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

REST made simple with Java