Introduction to JAX-RS

bjarlestam 4,733 views 33 slides Sep 14, 2009
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

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


Slide Content

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

Andreas Bjärlestam
mailto:[email protected]
http://andreas.bjarlestam.com
http://twitter.com/bjarlestam

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

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
•Use a Uniform Interface
• GET, PUT, DELETE, POST
•Use hypermedia
• Link your resources together
•Avoid session state
•Support Caching
•Communicate Representations
• Support different MIME Media Types
* This is a very simplified version of the real REST constraints, for the real stuff read Roy Fieldings dissertation

JAX-RS (JSR 311)
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("location")
public class LocationResouce {
@GET
@Produces("application/xml")
public String getLocation() {
return "<location>I’m in Sweden</location>" ;
}
}

curl

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

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

JAXB
@XmlRootElement
public class Location {
public String lattitude;
public String longitude;
public Date timestamp;
. . .
}

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

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

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

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/

JAX-RS
JSR 311
Version 1.0 was released in september 2008
Version 1.1 is planned to be part of JEE6

JAX-RS
Several open source implementations available
Sun Jersey
JBoss RestEasy
Restlets
Apache CXF
etc

Extra slides

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
–All the result pages 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