The document provides an introduction and overview of JAX-RS, the Java API for RESTful Web Services. It discusses what JAX-RS is, how resources are identified with URIs and represented in different formats, and how standard HTTP methods are used. It also covers JAX-RS annotations, sub-resources, request and response processing, and examples of implementing JAX-RS applications and resources.
7. Java API for RESTful Web Services
(JAX-RS)
Standard annotation-driven API that
aims to help developers build
RESTful Web services in Java
7
8. Status of JAX-RS
• JSR 311: JAX-RS 1.1 released Nov 2009
• Part of Java EE 6; Not part of the Web profile!
• Specifies integration with technologies:
– CDI 1.0
– EBJ 3.1
– Servlet 3.0
• Jersey implementation shipped with GlassFish 3.x
• 7 implementations
– Apache CXF, Apache Wink, eXo, Jersey, RESTEasy,
Restlet, Triaxrs
8
9. Sample JAX-RS application
GlassFish
hello.war
GET /hello/world HTTP/1.1
JAX-RS
resource
HTTP/1.1 200 OK
Content-Type: text/xml
<hello world/>
9
10. RESTful Application Cycle
Resources are identified by URIs
↓
Clients communicate with resources via requests using a
standard set of methods
↓
Requests and responses contain resource representations
in formats identified by media types
↓
Responses contain URIs that link to further resources
10
11. Resources are identified by URIs
http://example.com/widgets/foo
http://example.com/customers/bar
http://example.com/customers/bar/orders/2
http://example.com/orders/101230/customer
11
14. JAX-RS Sub-resources
• Root resources can declare sub-resources that will
match the unmatched part of the URI path
• Root resources implement sub-resource locator
methods
14
23. JAX-RS Sub-resources summary
• Sub-resource classes are processed at runtime
• Warning: easy to confuse sub-resource methods with
sub-resource locators
23
24. Standard Set of Methods
Method Purpose
GET Read, possibly cached
POST Update or create without a known ID
PUT Update or create with a known ID
DELETE Remove
24
25. JAX-RS Methods
• Annotate resource class methods with standard method
– @GET, @PUT, @POST, @DELETE, @HEAD
– @HttpMethod meta-annotation allows extensions, e.g.
WebDAV
• JAX-RS routes request to appropriate resource class and
method
• Flexible method signatures, annotations on parameters
specify mapping from request
• Return value mapped to response
25
27. Resource Representations
• Representation format identified by media type. E.g.:
– XML - application/properties+xml
– JSON - application/properties+json
– (X)HTML+microformats - application/xhtml+xml
• JAX-RS automates content negotiation
– GET /foo
Accept: application/properties+json
27
28. Resource Representations
• Annotate methods or classes with static capabilities
– @Produces, @Consumes
• Use Variant, VariantListBuilder and
Request.selectVariant for dynamic capabilities
– Also supports language and encoding
28
32. Responses Contain Links
• UriInfo provides information about deployment
context, the request URI and the route to the resource
• UriBuilder provides facilities to easily construct URIs
for resources
32
35. The future of JAX-RS
• Possible features in scope for a JAX-RS 2.0 effort
– Client API
– Declarative hyperlinking
– Model View Controller
– Quality of Source
– Form validation
– Asynchronous/Comet/WebSocket
– Improved integration with JSR-330 and @Inject
– JAX-RS Modules
35
39. 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
39