______/\\\\\\\\\\\_____/\\\\\\\\\_____/\\\_______/\\\________________/\\\\\\\\\_________/\\\\\\\\\\\___  _____\/////\\\///____/\\\\\\\\\\\\\__\///\\\___/\\\/_______________/\\\///////\\\_____/\\\/////////\\\_  _________\/\\\______/\\\/////////\\\___\///\\\\\\/________________\/\\\_____\/\\\____\//\\\______\///__  _________\/\\\_____\/\\\_______\/\\\_____\//\\\\_______/\\\\\\\\__\/\\\\\\\\\\\/______\////\\\_________  _________\/\\\_____\/\\\\\\\\\\\\\\\______\/\\\\______\////////___\/\\\//////\\\_________\////\\\______ _________\/\\\_____\/\\\/////////\\\______/\\\\\\_________________\/\\\____\//\\\___________\////\\\___ __/\\\___\/\\\_____\/\\\_______\/\\\____/\\\////\\\_______________\/\\\_____\//\\\___/\\\______\//\\\__ _\//\\\\\\\\\______\/\\\_______\/\\\__/\\\/___\///\\\_____________\/\\\______\//\\\_\///\\\\\\\\\\\/___ __\/////////_______\///________\///__\///_______\///______________\///________\///____\///////////_____ ____/\\\\\\\\\______/\\\\\\\\\\\\\\\_____/\\\\\\\\\\\_____/\\\\\\\\\\\\\\\_  __/\\\///////\\\___\/\\\///////////____/\\\/////////\\\__\///////\\\/////__  _\/\\\_____\/\\\___\/\\\______________\//\\\______\///_________\/\\\_______  _\/\\\\\\\\\\\/____\/\\\\\\\\\\\_______\////\\\________________\/\\\_______ _\/\\\//////\\\____\/\\\///////___________\////\\\_____________\/\\\_______ _\/\\\____\//\\\___\/\\\_____________________\////\\\__________\/\\\_______ _\/\\\_____\//\\\__\/\\\______________/\\\______\//\\\_________\/\\\_______ _\/\\\______\//\\\_\/\\\\\\\\\\\\\\\_\///\\\\\\\\\\\/__________\/\\\_______ _\///________\///__\///////////////____\///////////____________\///________ with 2009-09-15 Andreas Bjärlestam
Andreas Bjärlestam mailto:andreas@bjarlestam.com http://andreas.bjarlestam.com http://twitter.com/bjarlestam
REST? Attribution: http://www.flickr.com/photos/noahbulgaria/270090287
Pragmatic explanation Use HTTP the way it was designed to be used Take advantage of the good things in HTTP By following a set of constraints
Constraints in REST Give everything its own URI Communicate with a standard set of methods Communicate with Media Types Link your resources together Avoid session state Support caching * This is a very simplified version of the real REST constraints, for the real stuff read Roy Fieldings dissertation
JAX-RS (JSR 311) Java API for building RESTful services
JAX-RS Based on POJOs with Annotations
JAX-RS Makes the developer focus on URLs, HTTP methods and Media Types.
Lets code…
@Path @Path( "location" ) public   class  LocationResouce { }
@GET, @PUT, @POST @Path( &quot;location&quot; ) public   class  LocationResouce { @GET @Produces( &quot;application/xml&quot; ) public  String getLocation() { return   &quot;<location>I’m in Sweden</location>&quot; ; } }
curl
@PathParam, @QueryParam @Path( &quot;location&quot; ) public   class  LocationResouce { @GET @Path( &quot;{user}&quot; ) @Produces( &quot;application/xml&quot; ) public  String getLocation(@PathParam( &quot;user&quot; ) String user) { return   &quot;<location>”  + user +  &quot; is in Sweden</location>&quot; ; } }
JAXB @Path( &quot;location&quot; ) public   class  LocationResouce { @GET @Path( &quot;{user}&quot; ) @Produces( &quot;application/xml&quot; ) public  Location getLocation(@PathParam( &quot;user&quot; ) String user) { return new  Location( &quot;59.3&quot; ,  &quot;18&quot; ); } }
JAXB @XmlRootElement public   class  Location { public  String lattitude; public  String longitude; public  Date timestamp; . . . }
 
@Produces and Media Types @Path( &quot;location&quot; ) public   class  LocationResouce { @GET @Path( &quot;{user}&quot; ) @Produces({ &quot;application/xml&quot;, &quot;application/json&quot; }) public  Location getLocation(@PathParam( &quot;user&quot; ) String user) { return new  Location( &quot;59.3&quot; ,  &quot;18&quot; ); } }
 
@Context, UriInfo and UriBuilder @Path( &quot;location&quot; ) public   class  LocationResouce { @Context UriInfo uriInfo; . . . @POST @Consumes( &quot;application/x-www-form-urlencoded&quot; ) public  Response saveLocation(@FormParam( &quot;user&quot; ) String user) { locations.put (user,  new  Location( &quot;59.3&quot; ,  &quot;18&quot; )); UriBuilder uriBuilder =  uriInfo .getAbsolutePathBuilder(); URI userUri = uriBuilder.path(user).build(); return  Response.created(userUri).build(); } }
 
Cache Control @GET @Path( &quot;{user}&quot; ) @Produces({ &quot;application/xml&quot;, &quot;application/json&quot; }) public  Response getLocation(@PathParam( &quot;user&quot; ) String user) { Location l =  locations .get(user); CacheControl cc =  new   CacheControl(); cc.setMaxAge(500); Response.ResponseBuilder builder = Response.ok(l); builder.cacheControl(cc); return  builder.build() ; }
 
For the interested
For the interested Roy Fieldings PhD dissertation, see ch 5 http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm Roy Fieldings blog http://roy.gbiv.com/untangled/ Mark Hadleys blog http://weblogs.java.net/blog/mhadley/ Mark Nottinghams blog http://www.mnot.net Stefan Tilkovs blog http://www.innoq.com/blog/st/
Extra slides
JAX-RS JSR 311 Version 1.0 was finalized in september 2008 Version 1.1 is planned to be part of JEE6
JAX-RS Several implementations available Sun Jersey JBoss RestEasy Restlets Apache CXF Triaxrs Apache WINK
Serendipity By giving everything a URL, sticking to a Uniform Interface and linking your resources you make the chances of reuse in unexpected ways higher
Statelessness Better scaling No session storage on server Any server in a cluster can handle any request The resources can safely be cached More reliable A client can safely re-send a request Better resource reuse The resources can safely be linked to
Caching
Client Proxy Cache Client Client Server Proxy Cache Client Reverse Proxy Cache Client Cache Client Cache Client Cache Client Cache
“ The best requests are those that not even reach me.” - Anonymous overloaded Server
50 requests/second = 3000 requests/minute setting max-age=60 (seconds) can then save you 2999 requests
Caching GET can be cached while POST can't (safely) Specify max-age Use max-age cache control header Split data according to freshness requirement Support conditional GET Get if modified since E-tags

Introduction to JAX-RS

  • 1.
    ______/\\\\\\\\\\\_____/\\\\\\\\\_____/\\\_______/\\\________________/\\\\\\\\\_________/\\\\\\\\\\\___ _____\/////\\\///____/\\\\\\\\\\\\\__\///\\\___/\\\/_______________/\\\///////\\\_____/\\\/////////\\\_ _________\/\\\______/\\\/////////\\\___\///\\\\\\/________________\/\\\_____\/\\\____\//\\\______\///__ _________\/\\\_____\/\\\_______\/\\\_____\//\\\\_______/\\\\\\\\__\/\\\\\\\\\\\/______\////\\\_________ _________\/\\\_____\/\\\\\\\\\\\\\\\______\/\\\\______\////////___\/\\\//////\\\_________\////\\\______ _________\/\\\_____\/\\\/////////\\\______/\\\\\\_________________\/\\\____\//\\\___________\////\\\___ __/\\\___\/\\\_____\/\\\_______\/\\\____/\\\////\\\_______________\/\\\_____\//\\\___/\\\______\//\\\__ _\//\\\\\\\\\______\/\\\_______\/\\\__/\\\/___\///\\\_____________\/\\\______\//\\\_\///\\\\\\\\\\\/___ __\/////////_______\///________\///__\///_______\///______________\///________\///____\///////////_____ ____/\\\\\\\\\______/\\\\\\\\\\\\\\\_____/\\\\\\\\\\\_____/\\\\\\\\\\\\\\\_ __/\\\///////\\\___\/\\\///////////____/\\\/////////\\\__\///////\\\/////__ _\/\\\_____\/\\\___\/\\\______________\//\\\______\///_________\/\\\_______ _\/\\\\\\\\\\\/____\/\\\\\\\\\\\_______\////\\\________________\/\\\_______ _\/\\\//////\\\____\/\\\///////___________\////\\\_____________\/\\\_______ _\/\\\____\//\\\___\/\\\_____________________\////\\\__________\/\\\_______ _\/\\\_____\//\\\__\/\\\______________/\\\______\//\\\_________\/\\\_______ _\/\\\______\//\\\_\/\\\\\\\\\\\\\\\_\///\\\\\\\\\\\/__________\/\\\_______ _\///________\///__\///////////////____\///////////____________\///________ with 2009-09-15 Andreas Bjärlestam
  • 2.
    Andreas Bjärlestam mailto:andreas@bjarlestam.comhttp://andreas.bjarlestam.com http://twitter.com/bjarlestam
  • 3.
  • 4.
    Pragmatic explanation UseHTTP the way it was designed to be used Take advantage of the good things in HTTP By following a set of constraints
  • 5.
    Constraints in RESTGive everything its own URI Communicate with a standard set of methods Communicate with Media Types Link your resources together Avoid session state Support caching * This is a very simplified version of the real REST constraints, for the real stuff read Roy Fieldings dissertation
  • 6.
    JAX-RS (JSR 311)Java API for building RESTful services
  • 7.
    JAX-RS Based onPOJOs with Annotations
  • 8.
    JAX-RS Makes thedeveloper focus on URLs, HTTP methods and Media Types.
  • 9.
  • 10.
    @Path @Path( &quot;location&quot;) public class LocationResouce { }
  • 11.
    @GET, @PUT, @POST@Path( &quot;location&quot; ) public class LocationResouce { @GET @Produces( &quot;application/xml&quot; ) public String getLocation() { return &quot;<location>I’m in Sweden</location>&quot; ; } }
  • 12.
  • 13.
    @PathParam, @QueryParam @Path(&quot;location&quot; ) public class LocationResouce { @GET @Path( &quot;{user}&quot; ) @Produces( &quot;application/xml&quot; ) public String getLocation(@PathParam( &quot;user&quot; ) String user) { return &quot;<location>” + user + &quot; is in Sweden</location>&quot; ; } }
  • 14.
    JAXB @Path( &quot;location&quot;) public class LocationResouce { @GET @Path( &quot;{user}&quot; ) @Produces( &quot;application/xml&quot; ) public Location getLocation(@PathParam( &quot;user&quot; ) String user) { return new Location( &quot;59.3&quot; , &quot;18&quot; ); } }
  • 15.
    JAXB @XmlRootElement public class Location { public String lattitude; public String longitude; public Date timestamp; . . . }
  • 16.
  • 17.
    @Produces and MediaTypes @Path( &quot;location&quot; ) public class LocationResouce { @GET @Path( &quot;{user}&quot; ) @Produces({ &quot;application/xml&quot;, &quot;application/json&quot; }) public Location getLocation(@PathParam( &quot;user&quot; ) String user) { return new Location( &quot;59.3&quot; , &quot;18&quot; ); } }
  • 18.
  • 19.
    @Context, UriInfo andUriBuilder @Path( &quot;location&quot; ) public class LocationResouce { @Context UriInfo uriInfo; . . . @POST @Consumes( &quot;application/x-www-form-urlencoded&quot; ) public Response saveLocation(@FormParam( &quot;user&quot; ) String user) { locations.put (user, new Location( &quot;59.3&quot; , &quot;18&quot; )); UriBuilder uriBuilder = uriInfo .getAbsolutePathBuilder(); URI userUri = uriBuilder.path(user).build(); return Response.created(userUri).build(); } }
  • 20.
  • 21.
    Cache Control @GET@Path( &quot;{user}&quot; ) @Produces({ &quot;application/xml&quot;, &quot;application/json&quot; }) public Response getLocation(@PathParam( &quot;user&quot; ) String user) { Location l = locations .get(user); CacheControl cc = new CacheControl(); cc.setMaxAge(500); Response.ResponseBuilder builder = Response.ok(l); builder.cacheControl(cc); return builder.build() ; }
  • 22.
  • 23.
  • 24.
    For the interestedRoy Fieldings PhD dissertation, see ch 5 http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm Roy Fieldings blog http://roy.gbiv.com/untangled/ Mark Hadleys blog http://weblogs.java.net/blog/mhadley/ Mark Nottinghams blog http://www.mnot.net Stefan Tilkovs blog http://www.innoq.com/blog/st/
  • 25.
  • 26.
    JAX-RS JSR 311Version 1.0 was finalized in september 2008 Version 1.1 is planned to be part of JEE6
  • 27.
    JAX-RS Several implementationsavailable Sun Jersey JBoss RestEasy Restlets Apache CXF Triaxrs Apache WINK
  • 28.
    Serendipity By givingeverything a URL, sticking to a Uniform Interface and linking your resources you make the chances of reuse in unexpected ways higher
  • 29.
    Statelessness Better scalingNo session storage on server Any server in a cluster can handle any request The resources can safely be cached More reliable A client can safely re-send a request Better resource reuse The resources can safely be linked to
  • 30.
  • 31.
    Client Proxy CacheClient Client Server Proxy Cache Client Reverse Proxy Cache Client Cache Client Cache Client Cache Client Cache
  • 32.
    “ The bestrequests are those that not even reach me.” - Anonymous overloaded Server
  • 33.
    50 requests/second =3000 requests/minute setting max-age=60 (seconds) can then save you 2999 requests
  • 34.
    Caching GET canbe cached while POST can't (safely) Specify max-age Use max-age cache control header Split data according to freshness requirement Support conditional GET Get if modified since E-tags