REST made simple with Java

59
SDC 2008 REST made simple with Java

Transcript of REST made simple with Java

Page 1: REST made simple with Java

SDC 2008

REST made simple with Java

Page 2: REST made simple with Java

Niklas [email protected]://protocol7.com

http://twitter.com/protocol7

Page 3: REST made simple with Java

REST made simple with Java

Page 4: REST made simple with Java

REST?

Page 5: REST made simple with Java

HTTP 1.1

Page 6: REST made simple with Java

Some HTTP history

Page 7: REST made simple with Java

Some HTTP history

Page 8: REST made simple with Java

HTTP done right

Page 9: REST made simple with Java
Page 10: REST made simple with Java

Principles

Page 11: REST made simple with Java

PrinciplesEverything is a resource

Page 12: REST made simple with Java

PrinciplesA resource has an identifier

http://example.com/customers/1453

Page 13: REST made simple with Java

PrinciplesWe transfer representations

Page 14: REST made simple with Java

PrinciplesAll resources expose a uniform interface

GET, POST, PUT, DELETE

Page 15: REST made simple with Java

PrinciplesHypermedia as the engine of application state

Page 16: REST made simple with Java

PrinciplesClient-server, Stateless, Cacheable, Layered

Page 17: REST made simple with Java

Why?

Page 18: REST made simple with Java

Why?It's easy! Well, it's not

Page 19: REST made simple with Java

Why?It's what the cool kids use

Page 20: REST made simple with Java

Why?Web has been successful, copy!

Page 21: REST made simple with Java

Why?It's what others use

Page 22: REST made simple with Java

Why?Interoperability on the right level

Page 23: REST made simple with Java

Frameworks, yeay!

Page 24: REST made simple with Java

JAX-RS(aka JSR-311)

Page 25: REST made simple with Java

Jersey http://jersey.dev.java.net

Restlets http://www.restlet.org

RESTeasy http://www.jboss.org/resteasy

CXF http://cxf.apache.org

Page 26: REST made simple with Java

POJO basedAnnotation heavy

Page 27: REST made simple with Java

Resources

Page 28: REST made simple with Java

Code! Show me the code!

Page 29: REST made simple with Java

public class TimeReportService {

private TimeReportDao reportDao;

public TimeReport getReport(String username) { return reportDao.forUser(username); } public void saveReport(TimeReport report) { reportDao.update(report); }

...}

Page 30: REST made simple with Java

Request mapping

Page 31: REST made simple with Java

@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); }

...}

Page 32: REST made simple with Java
Page 33: REST made simple with Java

public class TimeReportApplication extends Application {

@Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<Class<?>>(); resources.add(TimeReportService.class);

return resources; }}

Page 34: REST made simple with Java

Path parameters and contexts

Page 35: REST made simple with Java

@Path("{username}/report")public class TimeReportService {

@GETpublic TimeReport getReport(@PathParam("username")

String username) { return reportDao.forUser(username); }

...}

http://example.com/niklas/report

Page 36: REST made simple with Java

@GETpublic TimeReport getReport(@Context SecurityContext sc) {

if(sc.isSecure()) {Principal user = sc.getUserPrincipal();return reportDao.forUser(user.getName());

} else { ... throw error, redirect to login } }

Page 37: REST made simple with Java

@GET public TimeReport getReport(@CookieParam("username")

String username) { return reportDao.forUser(username); }

Page 38: REST made simple with Java
Page 39: REST made simple with Java

Any media type is allowedXML, JSON, text/plain, Binary files, ...

Page 40: REST made simple with Java

Entity providersMessageBodyReader, MessageBodyWriter

Page 41: REST made simple with Java

Standard Entity providers

Page 42: REST made simple with Java
Page 43: REST made simple with Java

Writing your own Entity provider

Page 44: REST made simple with Java

BEGIN:VFREEBUSYDTSTART:20090324T080000ZDTEND:20090324T170000ZDTSTAMP:20090316T123136ZUID:[email protected]:SDC2009END:VFREEBUSY

Page 45: REST made simple with Java

@Provider @Produces("text/calendar")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("BEGIN:VFREEBUSY"); wr.println("DTSTART:" + DF.format(range.getStartTime())); wr.println("DTEND:" + DF.format(range.getEndTime())); wr.println("SUMMARY:" + range.getDescription()); wr.println("END:VFREEBUSY"); }

... }

Page 46: REST made simple with Java

Response, ResponseBuilder

Page 47: REST made simple with Java

Exception Mapping

Page 48: REST made simple with Java

Deployment

Page 49: REST made simple with Java

JAX-RS limitations

Page 50: REST made simple with Java

JAX-RS limitationsLifecycle support

Page 51: REST made simple with Java

JAX-RS limitationsWeak support for links, caching, method tunneling

Page 52: REST made simple with Java

JAX-RS limitationsAccept based content negotiation only

http://example.com/reporthttp://example.com/report.xmlhttp://example.com/report.calhttp://example.com/report.json

Page 53: REST made simple with Java

JAX-RS limitationsLimited security support

Page 54: REST made simple with Java

JAX-RS limitationsLeaky abstraction

Page 55: REST made simple with Java

Try it out!And have a look at the alternatives

Page 56: REST made simple with Java

Want more?

Page 57: REST made simple with Java

Questions?

Page 59: REST made simple with Java

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