Introduction to JAX-RS @ SIlicon Valley Code Camp 2010
Upcoming SlideShare
Loading in...5
×
 

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

on

  • 3,766 views

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

Statistics

Views

Total Views
3,766
Views on SlideShare
3,756
Embed Views
10

Actions

Likes
2
Downloads
132
Comments
1

3 Embeds 10

http://blogs.sun.com 8
http://safe.tumblr.com 1
http://bummerware.tumblr.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • JAX-RS
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010 Introduction to JAX-RS @ SIlicon Valley Code Camp 2010 Presentation Transcript

  • <Insert Picture Here> Introduction to JAX-RS Jitendra Kotamraju Oct 2010
  • What this presentation is about ? • Not the REST style per se – But, briefly ... • The basics of JAX-RS 2
  • 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 3 ©2010 Oracle Corporation
  • Very Short REST Primer: Buy This Book 4
  • REST is an Architectural Style Set of constraints you apply to the architecture of a distributed system to induce desirable properties 5 ©2010 Oracle Corporation
  • RESTful Web Services Application of REST architectural style to services that utilize Web standards (URIs, HTTP, HTML, XML, Atom, RDF etc.) 6 ©2010 Oracle Corporation
  • Java API for RESTful Web Services (JAX-RS) Standard annotation-driven API that aims to help developers build RESTful Web services in Java 7
  • 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
  • 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
  • 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
  • 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
  • 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 12 ©2010 Oracle Corporation
  • JAX-RS Resource URIs @Path("properties") public class Props { @GET List<Prop> getProperties(...) {...} } http://.../context/properties 13 ©2010 Oracle Corporation
  • 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
  • JAX-RS Sub-resources @Path("properties") public class Props { @GET @Path("{name}") Prop getProperty(@PathParam("name")String p){…} } GET http://.../context/properties/java.home 15 ©2010 Oracle Corporation
  • 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() {…} } 16 ©2010 Oracle Corporation
  • 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 17 ©2010 Oracle Corporation
  • 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 18 ©2010 Oracle Corporation
  • 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 19 ©2010 Oracle Corporation
  • 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 20 ©2010 Oracle Corporation
  • 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 21 ©2010 Oracle Corporation
  • 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 22 ©2010 Oracle Corporation
  • JAX-RS Sub-resources summary • Sub-resource classes are processed at runtime • Warning: easy to confuse sub-resource methods with sub-resource locators 23
  • 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
  • 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
  • JAX-RS HTTP Methods @Path("properties/{name}") public class Props { @GET Prop get(@PathParam("name") String name) {...} @PUT Prop set(@PathParam("name") String name, String value) {...} } 26 ©2010 Oracle Corporation
  • 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
  • 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
  • JAX-RS Resource Representations @GET @Produces("application/properties+xml") Prop getXml(@PathParam("name") String name) { ... } @GET @Produces("text/plain") String getText(@PathParam("name") String name) { ... } 29
  • JAX-RS Resource Representations @POST @Consumes("application/xml") @Produces({"application/xml","application/json"}) Customer getCustomer(Source id) { … return customer; } @XmlRootElement class Customer { public String first; public String last; } 30
  • Responses Contain Links HTTP/1.1 201 Created Date: Wed, 03 Jun 2009 16:41:58 GMT Server: Apache/1.3.6 Location: http://example.com/properties/foo Content-Type: application/order+xml Content-Length: 184 <property self="http://example.com/properties/foo"> <parent ref="http://example.com/properties/bar"/> <name>Foo</name> <value>1</value> </order> 31
  • 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
  • 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); 33 ©2010 Oracle Corporation
  • Demos 34
  • 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
  • Information • JSR-311: http://jsr311.dev.java.net/ • http://jersey.dev.java.net • mailto:users@jersey.dev.java.net • http://glassfish.dev.java.net 36
  • Questions ? 37
  • 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 38 ©2010 Oracle Corporation
  • 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