REST, which stands for 'Representational State Transfer', is a client-server architecture style of transferring representations of resources through requests and responses.
Following are the key attributes of REST architectural style.
1. Data and functionality are considered as REST resources.
2. REST Resources are accessed using URIs (Uniform Resource Identifiers), which are usually web links.
3. REST resources are represented by documents. Example - XML document, JSON document, HTML page, Image file etc.
4. REST Resources can be retrieved and updated using simple well-defined operations.
RESTful web services are simple, lightweight and loosely coupled web services used for creating APIs to REST resources, and are well suited for clients on the internet.
Following are the key principles of RESTful web services which make them lightweight and fast.
1. Resource access through URIs - A RESTful web service exposes a set of REST resources through URIs which are standard links on the Internet.
2. Stateless - RESTful web services are stateless and designed to be used with a stateless protocol, usually HTTP.
3. Uniform interface - A RESTful web service provides a standard set of four operations to create, read, update and delete resources - GET, PUT, POST and DELETE. These correspond to the GET, PUT, POST and DELETE methods of the HTTP protocol.
4. Multiple document formats - Resources are decoupled from their representation. So the same content can be accessed in a variety of formats such as XML, JSON, PDF, JPEG etc.
JAX-RS is a set of Java APIs, provided in Java EE, that are used for the development of RESTful web services.
Following are the key features of the JAX-RS API.
1. POJO-based API - The JAX-RS API provides a set of annotations and corresponding classes and interfaces that can be used with POJOs to expose them as web services.
2. HTTP-based API - The JAX-RS API is designed for HTTP as the base protocol. The API supports HTTP usage patters and provides mapping between HTTP actions and corresponding API classes and annotations.
3. Format Independent - The JAX-RS API is applicable to a wide variety of HTTP entity body content types.
4. Container Independent - JAX-RS applications can be deployed in a Java EE container, Servlet container or can be plugged in as a JAX-WS provider.
The JAX-RS API provides a set of annotations and its corresponding classes and interfaces that can be used on POJOs to expose them as RESTful web services.
Following are some key annotations defined in the JAX-RS API
@Path - @path annotation defines the relative URI path for the RESTful resource.
@GET - The @GET annotation is a request method designator. A Java method that is designated with this annotation will process HTTP GET requests.
@POST - The @POST annotation is a request method designator. A Java method that is designated with this annotation will process HTTP POST requests.
@PUT - The @PUT annotation is a request method designator. A Java method that is designated with this annotation will process HTTP PUT requests.
@DELETE - The @DELETE annotation is a request method designator. A Java method that is designated with this annotation will process HTTP DELETE requests.
@HEAD - The @HEAD annotation is a request method designator. A Java method that is designated with this annotation will process HTTP HEAD requests.
@PathParam - The @PathParam annotation is the URI path parameter that you can extract and use in your resource class.
@QueryParam - The @QueryParam annotation is the URI query parameters that you can extract and use in your resource class.
Root Resource classes are POJOs (Plain Old Java Objects) that are either annotated with @Path - or have at least one method annotated with @Path or a request method designator such as @GET, @POST, @PUT, @DELETE etc.
import javax.ws.rs.Path;
/**
* Root resource (exposed at 'service' path)
*/
@Path('service')
public class OrderService { ... }
Request method designator annotations are the runtime annotations @GET, @PUT, @POST, @DELETE and @HEAD, defined in the JAX-RS API, that can be applied to Java methods. Request method designator annotations correspond to the corresponding HTTP request methods GET, PUT, POST, DELETE and HEAD.
import javax.ws.rs.Path;
/**
* Root resource (exposed at 'myservice' path)
*/
@Path('myservice')
public class OrderService {
@GET
public String getOrder() {...}
}
The @Path annotation identifies the URI path template to which the resource responds and is specified at the class or method level of a resource.
URI path templates are URIs with variables embedded within the URI text.
import javax.ws.rs.Path;
/**
* Root resource (exposed at '/orders/' path)
*/
@Path('/orders/{orderNumber}')
public class OrderService {
@GET
public String getOrder() {...}
}
The @Produces annotation specifies the MIME media types or representations that a resource can produce and send back to the client.
If @Produces is applied at the class level, all the methods in a resource can produce the specified MIME types by default. If applied at the method level, the annotation overrides any @Produces annotations applied at the class level.
If no methods in a resource are able to produce the MIME type in a client request, the JAX-RS runtime sends back an HTTP '406 Not Acceptable' error.
import javax.ws.rs.Path;
/**
* Root resource (exposed at '/orders/' path)
*/
@Path('/orders/{orderNumber}')
public class OrderService {
@GET
@Produces('text/xml')
public String getOrder() {...}
}
The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. If @Consumes is applied at the class level, all the response methods accept the specified MIME types by default. If applied at the method level, @Consumes overrides any @Consumes annotations applied at the class level.
If a resource is unable to consume the MIME type of a client request, the JAX-RS runtime sends back an HTTP 415 ('Unsupported Media Type') error.
The value of @Consumes is an array of String of acceptable MIME types, or a comma-separated list of MediaType constants.
The JAX-RS API provides a high-level API for accessing JAX-RS services as well as other REST services. The JAX-RS Client API is defined in the javax.ws.rs.client package.
Following are the key steps to create a client request.
1. Obtain an instance of the javax.ws.rs.client.Client interface. The client instance can be created calling newClient() method javax.ws.rs.client.Client class.
2. Configure the client with the target URI to call. This is done by calling the target() method on the client instance and passing the target URI as a parameter.
3. Create a request based on the target URI and set the required attributes such as media type.
4. Call the request by invoking one of the request methods on the target service.
You can set HTTP headers on the request by calling the header() method on the client target.
You can add HTTP cookies to the request by calling the cookie() method on the client target, which takes a name-value pair as parameters.
You can register custom filters with the client request or the response received from the target resource. To register filter classes when the Client instance is created, call the Client.register method.
You can also register the filter classes on the target by calling WebTarget.register.
In the JAX-RS Client API, the Invocation.Builder.async() method is used when constructing a client request to indicate that the call to the service should be performed asynchronously. An asynchronous invocation returns control to the caller immediately, with a return type of java.util.concurrent.Future
A JAX-RS application consists of resource classes that are packaged within a war file. There are two ways to configure a JAX-RS application
1. By using the @ApplicationPath annotation in a subclass of javax.ws.rs.core.Application packaged within the WAR.
2. By using the servlet-mapping tag within the WAR's web.xml deployment descriptor.