2012年2月2日 星期四

JAX-RS Sub-resources

JAX-RS Sub-resources
Root resources can declare sub-resources that will
match the unmatched part of the URI path
  • Sub-resources method
    A sub-resource method is decorated with both a @Path
    annotation and one of the HTTP verb annotations. The subresource
    method is directly responsible for handling a request
    made on the resource using the specified HTTP verb.

  • Sub-resource locator
    Sub-resource locators are not decorated with one of the
    HTTP verb annotations and do not directly handle are request
    on the sub-resource. Instead, a sub-resource locator returns
    an instance of a resource class that can handle the request.
  • Sample
    @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
    GET http://.../context/properties/java.tmp.dir


What is JAX-RS?

















  • JSR-311: JAX-RS

  • Java API for RESTful Web Services is a Java programming language API that provides support in creating web services according to the Representational State Transfer (REST) architectural style.

  • JAX-RS is an official part of Java EE 6
    Exposes named RESOURCES which represent DATA
    Resource: POJO ( anything that is addressable over the Web. By addressable, resources that can be accessed and transferred between clients and servers)
    URI path (A Uniform Resource Identifier in a RESTful web service is a hyperlink to a resource )

  • Emphasis on simple, stateless point-to-point communication over HTTP
    Uses Uniform interfaces in HTTP Methods

  • GET/PUT/POST/DELETE
    • Annotation-driven
    • @Path
    • @Produces/Consumes
    • @PathParam/FormParam
    • @GET/POST/PUT/DELETE

  • Implementations of JAX-RS include:
    Apache CXF, an open source Web service framework.
    Jersey, the reference implementation from Sun (now Oracle).
    RESTEasy, JBoss's implementation.
    Apache Wink, Apache Software Foundation Incubator project, the server module implements JAX-RS.


  • Sample
    Google Data Protocol
    Many services at Google provide external access to data and
    functionality through APIs that utilize it
    (http://code.google.com/apis/gdata/)
    Amazon’s Simple Storage Service (S3)
    (http://aws.amazon.com/s3)
    Most of Yahoo!’s web services
    (http://developer.yahoo.com/)