Overview of JAX-RS 1.0 Features

compared with
Current by summerreflection
on Apr 21, 2009 15:00.

(show comment)
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (3)

View page history
h2. Creating a Root Resource Class
_Root resource classes_ are POJOs (Plain Old Java Objects) that are annotated with [@Path|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/Path.html] have at least one method annotated with [@Path|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/Path.html] or a _resource_ _method _request method designator_ annotation such as [@GET|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/GET.html], [@PUT|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/PUT.html], [@POST|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/POST.html], or [@DELETE|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/DELETE.html]. _Resource methods_ are methods of a resource class annotated with a resource request method designator. This section shows how to use Jersey to annotate Java objects to create RESTful web services.

The following code example is a very simple example of a root resource class using JAX-RS annotations. The example code shown here is from one of the samples that ships with Jersey, the zip file of which can be found in the maven repository [here | http://download.java.net/maven/2/com/sun/jersey/samples/helloworld/1.0-ea-SNAPSHOT/helloworld-1.0-ea-SNAPSHOT-project.zip].
h3. HTTP Methods
[@GET|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/GET.html], [@PUT|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/PUT.html], [@POST|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/POST.html] , [@DELETE|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/DELETE.html], and [@HEAD|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/HEAD.html] are _resource _request method designator_ annotations defined by JAX-RS and which correspond to the similarly named HTTP methods. In the example above, the annotated Java method will process HTTP GET requests. The behavior of a resource is determined by which of the HTTP methods the resource is responding to.

The following example is an extract from the storage service sample that shows the use of the PUT method to create or update a storage container:
If the path of the request URL is "printers" then the resource methods not annotated with [@Path|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/Path.html] will be selected. If the request path of the request URL is "printers/list" then first the root resource class will be matched and then the sub-resource methods that match "list" will be selected, which in this case is the sub-resource method {{getListOfPrinters}}. So in this example hierarchical matching on the path of the request URL is performed.
The second way [@Path|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/Path.html] may be used is on methods not annotated with resource request method designators such as [@GET|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/GET.html] or [@POST|https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/POST.html]. Such methods are referred to as sub-resource locators. The following example shows the method signatures for a root resource class and a resource class from the optimistic-concurrency sample:
{noformat}
@Path("/item")

The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact