• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Oredev 2009 JAX-RS
 

Oredev 2009 JAX-RS

on

  • 1,646 views

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

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

Statistics

Views

Total Views
1,646
Views on SlideShare
1,641
Embed Views
5

Actions

Likes
3
Downloads
40
Comments
0

1 Embed 5

http://www.slideshare.net 5

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Oredev 2009 JAX-RS Presentation Transcript

  • 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 used right
  • Constraints
  • Constraints Everything is a resource
  • Constraints A resource has an identifier http://example.com/reports/niklas
  • Constraints We transfer representations
  • Constraints All resources expose a uniform interface GET, POST, PUT, DELETE
  • Constraints Hypermedia as the engine of application state
  • Constraints 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!
  • 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("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); } ... }
  • Parameters
  • @Path("reports/ {username} ") public class TimeReportService { @GET public TimeReport getReport(@PathParam("username") String username) { return reportDao.forUser(username); } ... } http://example.com/report/ niklas
  • @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("username") String username) { return reportDao.forUser(username); }
  • Any media type is allowed XML, JSON, text/plain, binary, ...
  • Entity providers MessageBodyReader, MessageBodyWriter
  •  
  • Standard Entity providers
  • Writing your own Entity provider
  • BEGIN:VEVENT DTSTART:20091102T080000Z DTEND:20091106T170000Z SUMMARY:OREDEV END:VEVENT
  • @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;); } ... }
  • Request Response ResponseBuilder
  • Exception Mapping
  • Application
  •  
  • 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 ; } }
  • Deployment
  • JAX-RS limitations
  • JAX-RS limitations Lifecycle support
  • JAX-RS limitations Conneg 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
  • JAX-RS limitations Weak support for links
  • Try it out! And have a look at the alternatives
  • Want more? http://github.com/protocol7/jax-rs-demo
  • 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