JAX-RS Web Service Tutorial
In this tutorial, will demonstrates how RESTful services are created using JAX-RS, Tomcat will be primary application server. Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.
What are RESTful Web Services?
RESTful web services are built to work best on the Web. Representational State Transfer (REST) is
an architectural style that specifies constraints, such as the uniform
interface, that if applied to a web service induce desirable properties,
such as performance, scalability, and modifiability, that enable services to work best on the Web. In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs),
typically links on the Web. The resources are acted upon by using a set
of simple, well-defined operations. The REST architectural style
constrains an architecture to a client/server architecture and is
designed to use a stateless communication protocol, typically HTTP. In
the REST architecture style, clients and servers exchange
representations of resources by using a standardized interface and
The following principles encourage RESTful applications to be simple, lightweight, and fast:
- Resource identification through URI: A RESTful web service exposes a set of resources that identify the targets of the interaction with its clients. Resources are identified by URIs, which provide a global addressing space for resource and service discovery. See The @Path Annotation and URI Path Templates for more information.
- Uniform interface: Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE. PUT creates a new resource, which can be then deleted by using DELETE. GET retrieves the current state of a resource in some representation. POST transfers a new state onto a resource. See Responding to HTTP Methods and Requests for more information.
- Self-descriptive messages: Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others. Metadata about the resource is available and used, for example, to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control. See Responding to HTTP Methods and Requests and Using Entity Providers to Map HTTP Response and Request Entity Bodies for more information.
- Stateful interactions through hyperlinks: Every interaction with a resource is stateless; that is, request messages are self-contained. Stateful interactions are based on the concept of explicit state transfer. Several techniques exist to exchange state, such as URI rewriting, cookies, and hidden form fields. State can be embedded in response messages to point to valid future states of the interaction. See Using Entity Providers to Map HTTP Response and Request Entity Bodies and “Building URIs” in the JAX-RS Overview document for more information.
|Collection URI, such as ||List the URIs and perhaps other details of the collection's members.||Replace the entire collection with another collection.||Create a new entry in the collection. The new entry's URI is assigned automatically and is usually returned by the operation.||Delete the entire collection.|
|Element URI, such as ||Retrieve a representation of the addressed member of the collection, expressed in an appropriate Internet media type.||Replace the addressed member of the collection, or if it doesn't exist, create it.||Not generally used. Treat the addressed member as a collection in its own right and create a new entry in it.||Delete the addressed member of the collection.|