Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | arun-gupta |
View: | 2,970 times |
Download: | 1 times |
<Insert Picture Here>
Introduction to JAX-RS Jitendra Kotamraju
Oct 2010
2
What this presentation is about ?
• Not the REST style per se– But, briefly ...
• The basics of JAX-RS
3
Agenda
• What is JAX-RS ?
• JAX-RS Resources
• Standard Methods (uniform interface)
• JAX-RS Representations
• Lots of demos using JAX-RS API– @Path, HTTP methods, @Produces/@Consumes– XML, JSON, @xxParam, Response, Application,– Sub-resource locators, Injection etc
©2010 Oracle Corporation
4
Very Short REST Primer:Buy This Book
©2010 Oracle Corporation
5
REST is an Architectural Style
Set of constraints you apply to the architecture of a distributed system
to induce desirable properties
©2010 Oracle Corporation
6
RESTful Web Services
Application of REST architectural style to services that utilize Web standards (URIs, HTTP, HTML,
XML, Atom, RDF etc.)
7
Java API for RESTful Web Services (JAX-RS)
Standard annotation-driven API that aims to help developers build RESTful Web services in Java
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
9
Sample JAX-RS application
GlassFish
hello.war
JAX-RSresource
GET /hello/world HTTP/1.1
HTTP/1.1 200 OKContent-Type: text/xml
<hello world/>
10
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
RESTful Application Cycle
11
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
©2010 Oracle Corporation
12
JAX-RS Resources
• Resource == Java class– POJO, EJB Stateless, Singleton Session Beans
– 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
©2010 Oracle Corporation
13
JAX-RS Resource URIs
@Path("properties")public class Props { @GET List<Prop> getProperties(...) {...}
}
http://.../context/properties
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
©2010 Oracle Corporation
15
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("{name}") Prop getProperty(@PathParam("name")String p){…}}
GET http://.../context/properties/java.home
©2010 Oracle Corporation
16
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
©2010 Oracle Corporation
17
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
GET http://.../context/properties/java.home
©2010 Oracle Corporation
18
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
GET http://.../context/properties/java.home
©2010 Oracle Corporation
19
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
GET http://.../context/properties/java.home
©2010 Oracle Corporation
20
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
GET http://.../context/properties/java.tmp.dir
©2010 Oracle Corporation
21
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
GET http://.../context/properties/java.tmp.dir
©2010 Oracle Corporation
22
JAX-RS Sub-resources
@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}
@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}
GET http://.../context/properties/java.tmp.dir
23
JAX-RS Sub-resources summary
• Sub-resource classes are processed at runtime• Warning: easy to confuse sub-resource methods with
sub-resource locators
24
Standard Set of Methods
PurposeMethod
RemoveDELETE
Update or create with a known IDPUT
Update or create without a known IDPOST
Read, possibly cachedGET
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
©2010 Oracle Corporation
26
@Path("properties/{name}")public class Props {
@GET Prop get(@PathParam("name") String name) {...}
@PUT Prop set(@PathParam("name") String name, String value) {...}
}
JAX-RS HTTP Methods
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 /fooAccept: application/properties+json
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
29
@GET@Produces("application/properties+xml")Prop getXml(@PathParam("name") String name) { ...}
@GET@Produces("text/plain")String getText(@PathParam("name") String name) { ...}
JAX-RS Resource Representations
30
@POST@Consumes("application/xml")@Produces({"application/xml","application/json"})Customer getCustomer(Source id) { … return customer;}
@XmlRootElementclass Customer { public String first; public String last;}
JAX-RS Resource Representations
31
Responses Contain Links
HTTP/1.1 201 CreatedDate: Wed, 03 Jun 2009 16:41:58 GMTServer: Apache/1.3.6Location: http://example.com/properties/fooContent-Type: application/order+xmlContent-Length: 184
<property self="http://example.com/properties/foo"> <parent ref="http://example.com/properties/bar"/> <name>Foo</name> <value>1</value></order>
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
©2010 Oracle Corporation
33
Responses Contain Links
@Context UriInfo i;
SystemProperty p = ...UriBuilder b = i.getBaseUriBuilder();URI u = b.path(SystemProperties.class) .path(p.getName()).build();
List<URI> ancestors = i.getMatchedURIs();URI parent = ancestors.get(1);
34
Demos
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
36
Information
• JSR-311: http://jsr311.dev.java.net/• http://jersey.dev.java.net• mailto:[email protected]• http://glassfish.dev.java.net
37
Questions ?
©2010 Oracle Corporation
38
ServletJAX-RS application packaged in WAR like a servlet
For JAX-RS aware containersweb.xml can point to Application subclass
For non-JAX-RS aware containersweb.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
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 thenResource 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