Developing Restful Web Services With Jax-Rs: Marc Hadley Paul Sandoz
Developing Restful Web Services With Jax-Rs: Marc Hadley Paul Sandoz
Developing Restful Web Services With Jax-Rs: Marc Hadley Paul Sandoz
Marc Hadley
Paul Sandoz
Sun Microsystems, Inc
Agenda
http://example.com/widgets/foo
http://example.com/customers/bar
http://example.com/customers/bar/orders/2
http://example.com/orders/101230/customer
Resources are identified by URIs
> Resource == Java class
● POJO
● No required interfaces
> ID provided by @Path annotation
● Value is relative URI, base URI is provided by
deployment context or parent resource
● Embedded parameters for non-fixed parts of the
URI
● Annotate class or “sub-resource locator” method
Resources are identified by URIs
@Path("properties")
public class SystemProperties {
@GET
List<SystemProperty> getProperties(...) {...}
@Path("{name}")
SystemProperty getProperty(...) {...}
}
Standard Set of Methods
Method Purpose
DELETE Remove
Standard Set of Methods
@Path("properties/{name}")
public class SystemProperty {
@GET
Property get(@PathParam("name") String name)
{...}
@PUT
Property set(@PathParam("name") String name,
String value) {...}
}
Resource Representations
@GET
@Produces("application/properties+xml")
Property getXml(@PathParam("name") String name) {
...
}
@GET
@Produces("text/plain")
String getText(@PathParam("name") String name) {
...
}
Responses Contain Links
<property self="http://example.com/properties/foo">
<parent ref="http://example.com/properties/bar"/>
<name>Foo</name>
<value>1</value>
</order>
Responses Contain Links
@Context UriInfo i;
SystemProperty p = ...
UriBuilder b = i.getBaseUriBuilder();
URI u = b.path(SystemProperties.class)
.path(p.getName()).build();
SelectorThread st = GrizzlyServerFactory.create(
“http://127.0.0.1:8084/”, a);
Servlet
> JAX-RS application packaged in WAR like a
servlet
> For JAX-RS aware containers
● web.xml can point to Application subclass
> For non-JAX-RS aware containers
● web.xml points to implementation-specific
Servlet; and
● an init-param identifies the Application
subclass
> Resource classes and providers can access
Servlet request, context, config and response
via injection
Java EE
> Resource class can be an EJB session or
singleton bean
> Providers can be an EJB stateless session or
singleton bean
> JAX-RS annotations on local interface or no-
interface bean
> If JCDI (JSR 299) also supported then
● Resource classes can be JCDI beans
● Providers can be JCDI beans with application
scope
> Full access to facilities of native component
model, e.g. resource injection
Implementations
> Jersey
> Restlet
> JBoss RESTEasy
> Apache CXF
> Triaxrs
> Apache Wink
Agenda
> REST and JAX-RS Primer
> Deployment Options
> Demonstration
> Status
> Q&A
Agenda
> REST and JAX-RS Primer
> Deployment Options
> Demonstration
> Status
> Q&A
Status
Paul Sandoz
paul.sandoz@sun.com