<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




      ...
Agenda

            •      What is JAX-RS ?
            •      JAX-RS Resources
            •      Standard Methods (unifo...
Very Short REST Primer:
Buy This Book




                          4
REST is an Architectural Style



                    Set of constraints you apply to the
                    architecture...
RESTful Web Services



                      Application of REST architectural
                       style to services t...
Java API for RESTful Web Services
(JAX-RS)



Standard annotation-driven API that
    aims to help developers build
   RES...
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 in...
Sample JAX-RS application


                               GlassFish
                                 hello.war
   GET /he...
RESTful Application Cycle


             Resources are identified by URIs
                             ↓
 Clients communic...
Resources are identified by URIs


http://example.com/widgets/foo

http://example.com/customers/bar

http://example.com/cu...
JAX-RS Resources
            • Resource == Java class
                         – POJO, EJB Stateless, Singleton Session Be...
JAX-RS Resource URIs
            @Path("properties")
            public class Props {
              @GET
              Lis...
JAX-RS Sub-resources


• Root resources can declare sub-resources that will
  match the unmatched part of the URI path
• R...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {

                   @GET
           ...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources
            @Path("properties")
            public class Props {
              @GET
              @Pa...
JAX-RS Sub-resources summary




• Sub-resource classes are processed at runtime
• Warning: easy to confuse sub-resource m...
Standard Set of Methods



     Method                    Purpose

    GET       Read, possibly cached

    POST      Upda...
JAX-RS Methods

• Annotate resource class methods with standard method
   – @GET, @PUT, @POST, @DELETE, @HEAD
   – @HttpMe...
JAX-RS HTTP Methods

        @Path("properties/{name}")
        public class Props {

               @GET
               P...
Resource Representations

• Representation format identified by media type. E.g.:
   – XML - application/properties+xml
  ...
Resource Representations

• Annotate methods or classes with static capabilities
   – @Produces, @Consumes
• Use Variant, ...
JAX-RS Resource Representations

@GET
@Produces("application/properties+xml")
Prop getXml(@PathParam("name") String name) ...
JAX-RS Resource Representations

@POST
@Consumes("application/xml")
@Produces({"application/xml","application/json"})
Cust...
Responses Contain Links

HTTP/1.1 201 Created
Date: Wed, 03 Jun 2009 16:41:58 GMT
Server: Apache/1.3.6
Location: http://ex...
Responses Contain Links
• UriInfo provides information about deployment
  context, the request URI and the route to the re...
Responses Contain Links



              @Context UriInfo i;

              SystemProperty p = ...
              UriBuilde...
Demos




        34
The future of JAX-RS


• Possible features in scope for a JAX-RS 2.0 effort
      –   Client API
      –   Declarative hyp...
Information

•   JSR-311: http://jsr311.dev.java.net/
•   http://jersey.dev.java.net
•   mailto:users@jersey.dev.java.net
...
Questions ?




              37
Servlet
   JAX-RS application packaged in WAR like a servlet
   For JAX-RS aware containers
   web.xml can point to Applic...
Java EE

Resource class can be an EJB session or singleton bean
Providers can be an EJB stateless session or singleton
   ...
Upcoming SlideShare
Loading in...5
×

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

3,033

Published on

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,033
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
139
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

  1. 1. <Insert Picture Here> Introduction to JAX-RS Jitendra Kotamraju Oct 2010
  2. 2. What this presentation is about ? • Not the REST style per se – But, briefly ... • The basics of JAX-RS 2
  3. 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 3 ©2010 Oracle Corporation
  4. 4. Very Short REST Primer: Buy This Book 4
  5. 5. 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
  6. 6. 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
  7. 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. 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. 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. 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. 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
  12. 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 12 ©2010 Oracle Corporation
  13. 13. JAX-RS Resource URIs @Path("properties") public class Props { @GET List<Prop> getProperties(...) {...} } http://.../context/properties 13 ©2010 Oracle Corporation
  14. 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
  15. 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 15 ©2010 Oracle Corporation
  16. 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() {…} } 16 ©2010 Oracle Corporation
  17. 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 17 ©2010 Oracle Corporation
  18. 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 18 ©2010 Oracle Corporation
  19. 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 19 ©2010 Oracle Corporation
  20. 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 20 ©2010 Oracle Corporation
  21. 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 21 ©2010 Oracle Corporation
  22. 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 22 ©2010 Oracle Corporation
  23. 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. 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. 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
  26. 26. 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
  27. 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. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 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
  33. 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); 33 ©2010 Oracle Corporation
  34. 34. Demos 34
  35. 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
  36. 36. 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
  37. 37. Questions ? 37
  38. 38. 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
  39. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×