TSM_4_2012_en

Page 23

TODAY SOFTWARE MAGAZINE of JAX-RS. Other implementations would be: Apache CXR Restlet, JBOss RESTEasy.

A client-server implementation using Jersey

Let’s try to build a small application using jersey. An interface for “books” resource management will be provided by the application. I will also present a method of using the created web service, through the client support provided by Jersey. The server side will expose a series of useful methods for ”books” resource management: reset, get, list, delete and add. The client side will be created as a JUnit form and will expose a series of testing methods for validating the functionality of the Web service.

Project Creation

The project was implemented using SpringSource Tool Suite (including the WMware vFabric Web Server) OpenJDK 7 and Maven 3 in Ubuntu 12. Jersey needs four libraries (including their dependencies): • jersey-server: it contains Jersey’s implementation on server side • jersey-json: it contains JAXB support for Jersey • jersey-servlet: it contains support for servlets framework • jersey-client: it contains Jersey’s implementation on client side Using Jersey-Server together with Jersey- Client is not mandatory, according to the RESTful WebServices definition (this approach was only used to exemplify Jersey’s multiple features).

Maven and pom.xml

Maven offers a built-in method to resolve library dependencies, so the usage of this tool is highly recommended. For further details, see: https://github.com/ tavibolog/TodaySoftMag/blob/master/ pom.xml.

Web.xml

Every application installed in a servlet container needs more than a web.xml configuration folder. The servlet needed to initialize Jersey and the Restful services will be specified, along with the Java package as a parameter value of com.sun.jersey. config.property.packages:

<servlet> <servlet-name> Jersey REST Service </servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet. ServletContainer </servlet-class> <init-param> <param-name> com.sun.jersey.config.property.packages </param-name> <param-value> com.todaysoftmag.examples.rest </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> Jersey REST Service </servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>

and javax.ws.rs.code.* packages found in jersey-core dependency. Let’s discuss the most important. For service definition, you only need to define a class from the Java package, defined in web.xml by the com.sun.jersey. config.property.packages. In our case, this will be com.todaysoftmag.examples.rest. package com.todaysoftmag.examples. rest; // importurile sunt omise @Path(„/books”) public class BookService { … }

Servlet mapping specifies the URL pattern used to call the ServletContainer of Jersey. For a :

As you can see, class definition is preceeded by the@ Path annotation. This defines the way for the Web service. If we use the above example, The URL will be:

http://<server>:<ip>/<AppName>/ <JerseyDomain>/<WebService>/<method>

http://<server>:<ip>/<AppName>/ books/<method>

mapping refers to <JerseyDomain>. If we use “/*”in the URL pattern, the request will be as follows: http://<server>:<ip>/<AppName>/<WebSe rvice>/<method>

Or If we use ‘home’ in the URL pattern, the request will be as follows: http://<server>:<ip>/<AppName>/ home/<WebService>/<method>

How to define a domain class

The domain class defines the structure of the objects used by the Web services. As a rule, domain classes are POJO. The @X m lRo otE lement and @ XmlElement annotations are needed to enable JAXB automatic data mapping XML or JSON formats to/from POJO and vice versa. This support is offered by JAX-RS. If the domain class (Book in our case) defines a constructor, then the default constructor should be defined, being used in mapping by JXAB. @XmlRootElement public class Book { @XmlElement(name = „id”) String id; @XmlElement(name = „name”) String name; @XmlElement(name = „author”) String author; … }

The domain class complete code can be found here: https://github.com/ tavibolog/TodaySoftMag/blob/master/src/ main/java/com/todaysoftmag/examples/ rest/Book.java.

Service implementation

Annotations are widely used by JAX-RS (as mentioned above). Most of them can be found in the javax.ws.rs.*

The @Path annotation is also used for the methods exposed by the Web service: @PUT @Path(„/add”) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Book add(Book book) { …. }

In this case, the annotation value will be used as a part of the URL web service for that method. In our care, the URL for adding a ’book’ will be: http://<server>:<ip>/<AppName>/books/ add

This method is preceded by a series of annotations: • @PUT (similar to @GET, @POST, @DELETE): a request method designator which will process HTTP PUT request (or one that is similar, like HTTP GET, HTTP POST, HTTP DELETE) • @Consumes: used to specify the MIME types of representations a resource can produce. In this case, the ’add’ method will accept ’application/json’. A method might accept many MIME types separated by ’’,’’ when thay are included in the annotation, • @Produces: used to specify the MIME media types of representations a resource can produce. . In this case, the ’add’ method will produce ’application/json’. A method might accept many MIME types separated by ’’,’’ when thay are included in the annotation. There are some cases when we want to send parameters as a part of URL request. JAX-RS defines a series of annotations that can be used for this purpose. The most common are: www.todaysoftmag.com | nr. 4/2012

23


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.