REST services with Jersey and Spring 2.5
REST services are really useful : easy to write, easy to call and fast answers.
There are several implementations of JAX-RS (the Java 1.6 protocol for REST services):
- Spring 3
- RestEasy
- Restlet
- Jersey
- …
And maybe more, but I don't know them all and the majors are there.
For this article, I've choosen to use Jersey, the Glassfish implementation of JAX-RS coupled with Spring 2.5.6 and Maven2 (Maybe in a later post I'll speak about the Spring 3 implementation).
This article describes 3 parts:
- Maven configuration
- web.xml and applicationContext.xml configuration
- Development of the webservice
The Maven Configuration
The pom.xml must include at least 2 frameworks : Spring and Jersey, but we will also add the Lombox plugin (to automatically generate the getters/setters, toString, … methods) and the JSON framework.
For this tutorial, I used Maven 2. I let you read the pom.xml file included in the sources package.
It contains the dependencies for Spring and Jersey, plus the dependencies for Lombok, JSon and Jetty.
The web.xml Configuration
The web.xml file has juste to describe 2 parts : the Spring config file and the redirection for the webservice:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app id="jersey"> <display-name>jersey</display-name> <description>jersey</description> <!-- Spring configuration file --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- Start Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- REST Services --> <servlet> <servlet-name>Rest_Servlet</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Rest_Servlet</servlet-name> <!-- Redirect any calls to our jersey servlet --> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> |
That's all for the web.xml file, nothing to do more
The applicationContext.xml Configuration
The applicationcontext.xml file just have to ask Spring to find itself the webservices and to instanciate the correct beans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.codeartex.jersey.web" /> <!-- Bean WebService --> <bean id="wsTEst" class="com.codeartex.jersey.web.resources.WSTest" autowire="byName" /> </beans> |
A sample WebService
A Webservice is here just a new class and can encapsulate several services in one class
Each serv ice is just a method in this class. So it's very easy to add a new Webservice
The best practice is to add all the webservices of a same theme in the same class (i.e. a class for all the webservices for the users, a second class for all the webservices for the products, …)
Well, let's see how to declare this class with an example:
1 2 3 4 5 6 7 8 | @Path("/test") @Component //tell Spring it's a component, so it will parse it at the start of the application //(see the <context:component-scan base-package="com.codeartex.jersey.web" /> declaration in the applicationcontext.xml) @Scope("singleton") //tell Spring it's a singleton, so instanciate it only one time @Data //Lombok annotation to generate automatically the getter/setter/toString/equals/hashcode methods public class WSTest { //put here the methods... } |
A sample method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Webservice "hello" : return a string returning a "Hello {name}" String * @param name the name you want to show with the "Hello ..." * */ @GET //the path to cal this webservice: //http://xx.xx.xx.xx:8080/test/hello/Gwen //{name} is the first parameter. You can add parameters @Path("hello/{name}") @Produces("text/plain") public String getMagasinById(@PathParam("name") String name) { StringBuffer buffer = new StringBuffer(); buffer.append("Hello ").append(name); return buffer.toString(); } |
How to build and run the samples
To build the source with Maven, just run the following command:
mvn clean package
You can then execute it and test your webservices via Jetty (included in the pom.xml config file):
mvn jetty:run
Then, open a web browser window and enter the url:
http://localhost:8080/jersey/test/hello/YourName
No comments yet.