Introduction to JAX-RS

34
______/\\\\\\\\\\\_____/\\\\\\\\\_____/\\\_______/\\\________________/\\\\\\\\\_________/\\\\\\\\\\\___ _____\/////\\\///____/\\\\\\\\\\\\\__\///\\\___/\\\/_______________/\\\///////\\\_____/\\\/////////\\\_ _________\/\\\______/\\\/////////\\\___\///\\\\\\/________________\/\\\_____\/\\\____\//\\\______\///__ _________\/\\\_____\/\\\_______\/\\\_____\//\\\\_______/\\\\\\\\__\/\\\\\\\\\\\/______\////\\\_________ _________\/\\\_____\/\\\\\\\\\\\\\\\______\/\\\\______\////////___\/\\\//////\\\_________\////\\\______ _________\/\\\_____\/\\\/////////\\\______/\\\\\\_________________\/\\\____\//\\\___________\////\\\___ __/\\\___\/\\\_____\/\\\_______\/\\\____/\\\////\\\_______________\/\\\_____\//\\\___/\\\______\//\\\__ _\//\\\\\\\\\______\/\\\_______\/\\\__/\\\/___\///\\\_____________\/\\\______\//\\\_\///\\\\\\\\\\\/___ __\/////////_______\///________\///__\///_______\///______________\///________\///____\///////////_____ ____/\\\\\\\\\______/\\\\\\\\\\\\\\\_____/\\\\\\\\\\\_____/\\\\\\\\\\\\\\\_ __/\\\///////\\\___\/\\\///////////____/\\\/////////\\\__\///////\\\/////__ _\/\\\_____\/\\\___\/\\\______________\//\\\______\///_________\/\\\_______ _\/\\\\\\\\\\\/____\/\\\\\\\\\\\_______\////\\\________________\/\\\_______ _\/\\\//////\\\____\/\\\///////___________\////\\\_____________\/\\\_______ _\/\\\____\//\\\___\/\\\_____________________\////\\\__________\/\\\_______ _\/\\\_____\//\\\__\/\\\______________/\\\______\//\\\_________\/\\\_______ _\/\\\______\//\\\_\/\\\\\\\\\\\\\\\_\///\\\\\\\\\\\/__________\/\\\_______ _\///________\///__\///////////////____\///////////____________\///________ with 2009-09-15 Andreas Bjärlestam

description

An introduction to RESTful web services in Java using JAX-RS

Transcript of Introduction to JAX-RS

Page 1: Introduction to JAX-RS

______/\\\\\\\\\\\_____/\\\\\\\\\_____/\\\_______/\\\________________/\\\\\\\\\_________/\\\\\\\\\\\___ _____\/////\\\///____/\\\\\\\\\\\\\__\///\\\___/\\\/_______________/\\\///////\\\_____/\\\/////////\\\_ _________\/\\\______/\\\/////////\\\___\///\\\\\\/________________\/\\\_____\/\\\____\//\\\______\///__ _________\/\\\_____\/\\\_______\/\\\_____\//\\\\_______/\\\\\\\\__\/\\\\\\\\\\\/______\////\\\_________ _________\/\\\_____\/\\\\\\\\\\\\\\\______\/\\\\______\////////___\/\\\//////\\\_________\////\\\______ _________\/\\\_____\/\\\/////////\\\______/\\\\\\_________________\/\\\____\//\\\___________\////\\\___ __/\\\___\/\\\_____\/\\\_______\/\\\____/\\\////\\\_______________\/\\\_____\//\\\___/\\\______\//\\\__ _\//\\\\\\\\\______\/\\\_______\/\\\__/\\\/___\///\\\_____________\/\\\______\//\\\_\///\\\\\\\\\\\/___ __\/////////_______\///________\///__\///_______\///______________\///________\///____\///////////_____

____/\\\\\\\\\______/\\\\\\\\\\\\\\\_____/\\\\\\\\\\\_____/\\\\\\\\\\\\\\\_ __/\\\///////\\\___\/\\\///////////____/\\\/////////\\\__\///////\\\/////__ _\/\\\_____\/\\\___\/\\\______________\//\\\______\///_________\/\\\_______ _\/\\\\\\\\\\\/____\/\\\\\\\\\\\_______\////\\\________________\/\\\_______ _\/\\\//////\\\____\/\\\///////___________\////\\\_____________\/\\\_______ _\/\\\____\//\\\___\/\\\_____________________\////\\\__________\/\\\_______ _\/\\\_____\//\\\__\/\\\______________/\\\______\//\\\_________\/\\\_______ _\/\\\______\//\\\_\/\\\\\\\\\\\\\\\_\///\\\\\\\\\\\/__________\/\\\_______ _\///________\///__\///////////////____\///////////____________\///________

with

2009-09-15Andreas Bjärlestam

Page 2: Introduction to JAX-RS

Andreas Bjärlestammailto:[email protected]://andreas.bjarlestam.comhttp://twitter.com/bjarlestam

Andreas Bjärlestammailto:[email protected]://andreas.bjarlestam.comhttp://twitter.com/bjarlestam

Page 3: Introduction to JAX-RS

Attribution: http://www.flickr.com/photos/noahbulgaria/270090287

REST?REST?

Page 4: Introduction to JAX-RS

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

Page 5: Introduction to JAX-RS

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

Page 6: Introduction to JAX-RS

JAX-RS (JSR 311)

Java API for building RESTful services

Page 7: Introduction to JAX-RS

JAX-RS

Based on POJOs with Annotations

Page 8: Introduction to JAX-RS

JAX-RS

Makes the developer focus on URLs, HTTP methods and Media Types.

Page 9: Introduction to JAX-RS

Lets code…

Page 10: Introduction to JAX-RS

@Path

@Path("location")

public class LocationResouce {

}

Page 11: Introduction to JAX-RS

@GET, @PUT, @POST

@Path("location")

public class LocationResouce {

@GET

@Produces("application/xml")

public String getLocation() {

return "<location>I’m in Sweden</location>";

}

}

Page 12: Introduction to JAX-RS

curl

Page 13: Introduction to JAX-RS

@PathParam, @QueryParam

@Path("location")

public class LocationResouce {

@GET

@Path("{user}")

@Produces("application/xml")

public String getLocation(@PathParam("user") String user) {

return "<location>” + user + " is in Sweden</location>";

}

}

Page 14: Introduction to JAX-RS

JAXB

@Path("location")

public class LocationResouce {

@GET

@Path("{user}")

@Produces("application/xml")

public Location getLocation(@PathParam("user") String user) {

return new Location("59.3", "18");

}

}

Page 15: Introduction to JAX-RS

JAXB

@XmlRootElement

public class Location {

public String lattitude;

public String longitude;

public Date timestamp;

. . .

}

Page 16: Introduction to JAX-RS
Page 17: Introduction to JAX-RS

@Produces and Media Types

@Path("location")

public class LocationResouce {

@GET

@Path("{user}")

@Produces({"application/xml", "application/json"})

public Location getLocation(@PathParam("user") String user) {

return new Location("59.3", "18");

}

}

Page 18: Introduction to JAX-RS
Page 19: Introduction to JAX-RS

@Context, UriInfo and UriBuilder

@Path("location")

public class LocationResouce {

@Context UriInfo uriInfo;

. . .

@POST

@Consumes("application/x-www-form-urlencoded")

public Response saveLocation(@FormParam("user") String user) {

locations.put(user, new Location("59.3", "18"));

UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();

URI userUri = uriBuilder.path(user).build();

return Response.created(userUri).build();

}

}

Page 20: Introduction to JAX-RS
Page 21: Introduction to JAX-RS

Cache Control

@GET

@Path("{user}")

@Produces({"application/xml", "application/json"})

public Response getLocation(@PathParam("user") 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();

}

Page 22: Introduction to JAX-RS
Page 23: Introduction to JAX-RS

For the interested

Page 24: Introduction to JAX-RS

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/

Page 25: Introduction to JAX-RS

Extra slides

Page 26: Introduction to JAX-RS

JAX-RS

JSR 311

Version 1.0 was finalized in september 2008

Version 1.1 is planned to be part of JEE6

Page 27: Introduction to JAX-RS

JAX-RS

Several implementations available

Sun Jersey

JBoss RestEasy

Restlets

Apache CXF

Triaxrs

Apache WINK

Page 28: Introduction to JAX-RS

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

Page 29: Introduction to JAX-RS

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

Page 30: Introduction to JAX-RS

Caching

Page 31: Introduction to JAX-RS

Client

ProxyCache

Client

Client

Server

ProxyCache

Client

ReverseProxyCache

ClientCache

ClientCache

ClientCache

ClientCache

Page 32: Introduction to JAX-RS

“The best requests are those that not even reach me.”

- Anonymous overloaded Server

Page 33: Introduction to JAX-RS

50 requests/second=

3000 requests/minute

setting max-age=60 (seconds)can then save you 2999 requests

Page 34: Introduction to JAX-RS

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