Wednesday, 15 May 2013

JAX-RS RESTful web service using Jersey

JAX-RS RESTful web service using Jersey

JAX-RS RESTful web service using Jersey

JAX-RS provides the functionality for Representational State Transfer (RESTful) web services.
RESTful web services often better integrated with HTTP than SOAP based service. Which does not require
XML message or wsdl service.
There are two most important implementation of JAX-RS Java API are Jersey and Resteasy
What Are RESTful Web Services?
RESTful web services are built to work best on the Web. Representational State Transfer (REST) is a
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 protocol.
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.

There are some annotations implemented by Jersy and Resteasy
@Path : The @Path annotation’s value is a relative URI path indicating where the Java class will be hosted:
for example, /helloworld. You can also embed variables in the URIs to make a URI path template. For example,
you could ask for the name of a user and pass it to the application as a variable in the
URI: /helloworld/{username}.
@GET :The @GET annotation is a request method designator and corresponds to the similarly named HTTP method.
The Java method annotated with this request method designator will process HTTP GET requests. The behavior of a
resource is determined by the HTTP method to which the resource is responding.
@POST :The @POST annotation is a request method designator and corresponds to the similarly named HTTP method.
The Java method annotated with this request method designator will process HTTP POST requests. The behavior of a
resource is determined by the HTTP method to which the resource is responding.
@PUT : The @PUT annotation is a request method designator and corresponds to the similarly named HTTP method.
The Java method annotated with this request method designator will process HTTP PUT requests. The behavior of
a resource is determined by the HTTP method to which the resource is responding.
@DELETE : The @DELETE annotation is a request method designator and corresponds to the similarly named
HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests.
The behavior of a resource is determined by the HTTP method to which the resource is responding.
@HEAD : The @HEAD annotation is a request method designator and corresponds to the similarly named HTTP method.
The Java method annotated with this request method designator will process HTTP HEAD requests. The behavior of a
resource is determined by the HTTP method to which the resource is responding.
@PathParam : The @PathParam annotation is a type of parameter that you can extract for use in your resource class.
URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template
variable names specified in the @Path class-level annotation.
@QueryParam : The @QueryParam annotation is a type of parameter that you can extract for use in your resource
class. Query parameters are extracted from the request URI query parameters.
@Consumes : The @Consumes annotation is used to specify the MIME media types of representations a resource can
consume that were sent by the client.
@Produces : The @Produces annotation is used to specify the MIME media types of representations a resource can
produce and send back to the client: for example, "text/plain".
@Provider : The @Provider annotation is used for anything that is of interest to the JAX-RS runtime,
such as MessageBodyReader and MessageBodyWriter. For HTTP requests, the MessageBodyReader is used to map an
HTTP request entity body to method parameters. On the response side, a return value is mapped to an HTTP
response entity body by using a MessageBodyWriter. If the application needs to supply additional metadata,
such as HTTP headers or a different status code, a method can return a Response that wraps the entity and
that can be built using Response.ResponseBuilder.

JAX-RS RESTful web service using Jersey Hello World Example

Where Jersy is reference implementation of JAX-RS Java API. Jersy is used to build RESTful web service.
Dependency of application are :
Jersey 1.8
JDK 1.6
Tomcat 6.0
Eclipse 3.6
Step 1 : Create REST Service

Step 2 : web.xml
In web.xml, register com.sun.jersey.spi.container.servlet.ServletContainer, and puts your Jersey
service folder under init-param, com.sun.jersey.config.property.packages.

In this example, web request from appname/rest/hello/ will match to HelloWorldService, via @Path("/hello").
And the {any values} from appname/rest/hello/{any values} will match to parameter annotated with @PathParam.
URL : http://localhost:8080/appname/rest/hello/wakil

When Run the above url in your browser, the output will come as
Jersey say wakil


No comments:

Post a Comment