http://www.developer.com/java/article.php/3843846/JAX-RS-The-Java-API-for-RESTful-Web-Services.htm
JAX-RS (JSR 311), the Java API for RESTful web services, has had a profound effect on the architecture and design of web services. It was inspired by Roy Thomas Fielding's dissertation on the "Design of Network-based Software Architectures', in which he introduced the REST (Representational State Transfer) concept. This introductory article to JAX-RS reviews two popular JAX-RS frameworks, Jersey and Apache CXF, and provides some basic examples that illustrate how to build, deploy, and execute RESTful web services. Before diving into the formal definition of REST, let's walk through the basic concepts required to understand REST. You also should review Java annotations (part of JDK 1.4) and the Maven configuration and build tool, if you are not familiar with them already. The acronym REST (Representational State Transfer) refers to an architectural style for distributed hypermedia systems. (The term hypermedia refers to a combination of text, hyperlinks, graphics, audio, and video that creates an interactive medium of information. A network-distributed hypermedia system is a system of interlinked hypertext documents accessed via the Internet, such as the World Wide Web.) REST proposes a set of architectural principles, including client-stateless-server communication, uniform interface, separation of concerns and layered systems (click here for definitions of these principles). When applied as a whole to web services, the resulting systems generally achieve high performance, scalability, low/simplified component interactions, enforced security, and encapsulation of legacy systems. A resource class is a Java class annotated with JAX-RS annotations to represent a web resource. A root resource class is a POJO (plain old Java object) that is annotated with @Path, has at least one method annotated with @Path, or a request method designator such as @GET, @PUT, @POST, or @DELETE to handle requests on the corresponding resource. Resource methods are request methods of a resource class. Annotated with @Path, they handle resource-specific operations: creating, reading, updating, and deleting resources. When a request method designator is used on resource methods, it is referred to as a sub-resource method. Methods not annotated with resource method designators are referred to as sub-resource locators. We will look at examples later in the article. JAX-RS is divided into three packages:
Here's what you need to know when implementing JAX-RS:
Let's look at two popular REST frameworks that provide standard ways for building RESTful services in Java:
Jersey supports the annotations defined in JSR-311 to enable you to build RESTful web services that are simple and lightweight. The JAX-RS API provides a set of annotations and associated classes/interfaces that you can use to expose POJOs as web services. Jersey also provides additional functionality beyond the JAX-RS API, including support for JavaScript Object Notation (JSON), Spring framework integration, and the Web Application Description Language (WADL). Let's build a simple web service to demonstrate the use of annotations in JSR 311. The following example uses the Grizzly Web Container in Eclipse 3.3.2 with the plugin for Maven 2.0.9 installed. You need to have JDK 1.5 or higher installed to follow along.
Type in the URI JAX-RS annotations are runtime annotations. Let's look at some of the important ones that you implemented in the previous example. For a complete list of annotations defined in JAX-RS, refer to the Java API for JAX-RS documentation. Currently, there are two ways to build RESTful services with CXF: Download the source for the latest distribution of Apache CXF, which includes JAX-RS examples in the Follow the process outlined for Jersey in the previous section to build a Maven template in Eclipse (choose the Group Id and Artifact Id as listed in the POM of the sample distribution). After you have created the project, make sure the package structure reflects the one shown in the sample. Next, copy/extract the files from the distribution into the Eclipse workspace, including the POM (replacing the existing POM in the Eclipse/Maven project). The sample at On the server side: On the client side: REST is an architectural style that provides a simple and uniform way of calling server-side resources using HTTP web services. Developing web services has never been easy, but REST assists the developer with an inherent loose coupling of client and server without the complications of WSDL or XML. In this article, we have covered the basic definition of REST and demonstrated two frameworks that support REST concepts. Jersey User Guide
JAX-RS: The Java API for RESTful Web Services
October 14, 2009
Introduction
What Is REST?
JAX-RS Terminology
JAX-RS API and Implementation Details
javax.ws.rs.core.Response, javax.ws.rs.core.GenericEntity, void or any other Java type, and be declared public. These return types are mapped appropriately into status codes by the JAX-RS Provider. For example, a return type of void is mapped into the 204 status code.
javax.ws.rs.WebApplicationException) inside a sub-resource method or sub-resource locator.
javax.ws.rs.ext.MessageBodyWriter and javax.ws.es.ext.MessageBodyReader are responsible for serializing and deserializing between stream and Java types. REST Frameworks
Jersey Framework
A Simple JAX-RS Web Service with Jersey
myrestapp.
com.mycompany.resources in the project and add a root resource class (RestByExampleResouce.java) that contains implementations for sub-resource methods in the resources package. We will discuss the methods later in the article.
DeployRestByExample.java) in the myrestapp package to deploy the root resource (RestByExample.java) to the Grizzly web container programmatically using Grizzly-specific classes such as com.sun.grizzly.http.SelectorThread. You can accomplish this in other ways as well. A web service may extend the Application class to declare root resource and provider classes. Here is an example:
Alternatively, you could reuse the Jersey implementation com.sun.jersey.api.core.PackagesResourceConfig, which scans for root resource and provider classes given a classpath or a set of package names. Here is an example:
myrestapp package under src/test/java, as shown here.

http://localhost:9998/restbyexample in your favorite browser to try the new web service you just implemented. Remember that URI names are case-sensitive.JAX-RS Annotations
/restbyexample'.getUserName() has an annotation @GET @Path("users/{name}"). It responds to the URI http://localhost:9998/restbyexample/users/xx by returning the text: You asked for xxdoGetAsText() and doGetAsHtml() produce representations identified by the MIME media types text/plain and text/html, respectively.doPostMimeFormEncoded() will consume representations identified by the MIME media type application/x-www-form-urlencoded.doPostMimeFormEncoded() will retrieve the form parameter 'whoami' when the form is posted with the action "http://localhost:9998/restbyexample/formpost".doPostMimeFormEncoded() served to process HTTP POST.Apache-CXF
A Simple JAX-RS Web Service with Apache-CXF
jax-rs directory under samples and a POM for Maven builds. Please review the README in the samples directory.C:\apacheSourceREST\apache-cxf-2.2.3-src\distribution\src\main\release\samples\jax_rs\basic shows how to build REST-based web services using JAX-RS. It has two parts: a server and a client.
CustomerService.java demonstrates how to build a RESTful web service with the JAX-RS (JSR-311) APIs./customerservice/ is denoted by @Path at the class level. It responds to GET at the sub URI /customers/{id}/), PUT at /customers/, POST at /customers/, and DELETE at /customers/{id}/.getOrder(@PathParam("orderId") String orderId) is an example of a sub-resource locator because it doesn't use a request method designator.http://localhost:9000/customerservice/customers/123 returns an XML document representing the customer 123. The annotation @XmlRootElement defines an XML element name for the XML schema type of the corresponding classes (Customer, Order, Product) using JAXB.
Conclusion
References
Grizzly
JSR 311 JAX-RS specification
JAX-RS API
Jersey Client API
Apache CXF
Apache CXF API
Source Code