Oredev 2009 JAX-RS

Embed Size (px)

Citation preview

SDC 2008

REST made simple with Java

Niklas [email protected]://protocol7.comhttp://twitter.com/protocol7

From Gteborg

Callista

REST made simple with Java

REST?

From Gteborg

Callista

HTTP 1.1

Some HTTP history

From Gteborg

Callista

Some HTTP history

From Gteborg

Callista

HTTP used right

From Gteborg

Callista

Constraints

From Gteborg

Callista

ConstraintsEverything is a resource

From Gteborg

Callista

ConstraintsA resource has an identifier

http://example.com/reports/niklas

From Gteborg

Callista

ConstraintsWe transfer representations

From Gteborg

Callista

ConstraintsAll resources expose a uniform interface

GET, POST, PUT, DELETE

From Gteborg

Callista

ConstraintsHypermedia as the engine of application state

From Gteborg

Callista

ConstraintsClient-server, Stateless, Cacheable, Layered

From Gteborg

Callista

Why?

From Gteborg

Callista

Why?It's easy!

Well, it's not

From Gteborg

Callista

Why?It's what the cool kids use

From Gteborg

Callista

Why?Web has been successful, copy!

From Gteborg

Callista

Why?It's what others use

From Gteborg

Callista

Why?Interoperability on the right level

From Gteborg

Callista

Frameworks!

From Gteborg

Callista

JAX-RS(aka JSR-311)

From Gteborg

Callista

Jersey http://jersey.dev.java.netRestlets http://www.restlet.orgRESTeasy http://www.jboss.org/resteasyCXF http://cxf.apache.org

From Gteborg

Callista

POJO basedAnnotation heavy

From Gteborg

Callista

Resources

From Gteborg

Callista

Code! Show me the code!

From Gteborg

Callista

public class TimeReportService {

private TimeReportDao reportDao;

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

...}

From Gteborg

Callista

Request mapping

From Gteborg

Callista

From Gteborg

Callista

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

...}

From Gteborg

Callista

Parameters

From Gteborg

Callista

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

@GETpublic TimeReport getReport(@PathParam("username") String username) { return reportDao.forUser(username); }

...}

http://example.com/report/niklas

From Gteborg

Callista

@GETpublic TimeReport getReport(@Context SecurityContext sc) {if(sc.isSecure()) {Principal user = sc.getUserPrincipal();return reportDao.forUser(user.getName());} else { ... throw error, redirect to login } }

From Gteborg

Callista

@GET public TimeReport getReport(@CookieParam("username") String username) { return reportDao.forUser(username); }

From Gteborg

Callista

Any media type is allowedXML, JSON, text/plain, binary, ...

From Gteborg

Callista

Entity providersMessageBodyReader, MessageBodyWriter

From Gteborg

Callista

From Gteborg

Callista

Standard Entity providers

From Gteborg

Callista

Writing your own Entity provider

From Gteborg

Callista

BEGIN:VEVENTDTSTART:20091102T080000ZDTEND:20091106T170000ZSUMMARY:OREDEVEND:VEVENT

From Gteborg

Callista

@Provider @Produces("text/calendar")public class TimeReportICalWriter implements MessageBodyWriter {

public void writeTo(TimeReport t, Class type, Type genericType,Annotation[] annotations,MediaType mediaType,MultivaluedMap httpHeaders, OutputStream entityStream) {

PrintWriter wr = new PrintWriter(entityStream); ... for(TimeRange range : t.getRanges()) { wr.println("BEGIN:VEVENT"); wr.println("DTSTART:" + DF.format(range.getStartTime())); wr.println("DTEND:" + DF.format(range.getEndTime())); wr.println("SUMMARY:" + range.getDescription()); wr.println("END:VEVENT"); }

... }

From Gteborg

Callista

RequestResponseResponseBuilder

From Gteborg

Callista

Exception Mapping

From Gteborg

Callista

Application

From Gteborg

Callista

From Gteborg

Callista

public class TimeReportApplication extends Application {

@Override public Set> classes = new HashSet