Google

May 8, 2014

RESTEasy tutorial with Spring, Maven, and Eclipse

This assumes that you have gone through the RESTEasy tutorial to create RESTFul web services with Maven and Eclipse.


Step 1: In this tutorial, we are going to use RESTeasy with Spring. So, the pom.xml file needs to have Spring jar dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany</groupId>
 <artifactId>RESTfulWebService</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>RESTfulWebService Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <build>
  <finalName>RESTfulWebService</finalName>
 </build>

 <properties>
  <resteasy.version>2.3.6.Final</resteasy.version>
  <spring.version>3.1.0.RELEASE</spring.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>


  <!-- JAX-RS dependencies -->
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>jaxrs-api</artifactId>
   <version>${resteasy.version}</version>
  </dependency>
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-jaxrs</artifactId>
   <version>${resteasy.version}</version>
  </dependency>
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-jaxb-provider</artifactId>
   <version>${resteasy.version}</version>
  </dependency>

  <!-- JAX-RS -->
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-spring</artifactId>
   <scope>runtime</scope>
   <version>${resteasy.version}</version>
  </dependency>


  <!-- Spring Dependencies -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <scope>compile</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <scope>compile</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <scope>compile</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <scope>runtime</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-expression</artifactId>
   <scope>runtime</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <scope>runtime</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <scope>compile</scope>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <scope>test</scope>
   <version>${spring.version}</version>
  </dependency>

 </dependencies>

</project>


Note: resteasy-spring and other org.springframework libraries.




Step 2: The files that are getting modified or added are highlighted in green.



Step 3: Add the Java interfaces and the classes as shown below.

Firstly, the Web Service interface that talks HTTP protocol. This interface has the web service annotations

package com.mycompany;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/simple")
public interface SimpleWebService {

 @GET
 @Path("/{param}")
 public abstract Response printMessage(@PathParam("param") String msg);
}


Secondly, the web service implementation, that makes use of plan POJO class for the business logic, and this plain service (i.e. protocol agnostic) class will be dependency injected via Spring file applicationContext.xml under src/main/resources/com/mycompany.

package com.mycompany;

import javax.ws.rs.core.Response;

public class SimpleWebServiceImpl implements SimpleWebService {

 private SimpleService service;
 
 public SimpleWebServiceImpl(){}
 
 public SimpleWebServiceImpl(SimpleService service) {
  this.service = service;
 }

 public Response printMessage(String msg) {
  String result = service.getMessage(msg);
  return Response.status(200).entity(result).build();
 }

}


Define the SimpleService  POJO interface  (i.e. PROTOCOL agnostic) that is injected in to the Web Service.

package com.mycompany;

public interface SimpleService {
 
 public abstract String getMessage( String msg);
}



The corresponsing implementation class that has the business logic. In this case just prepends "Hello : " to the msg.

package com.mycompany;

public class SimpleServiceImpl implements SimpleService{

 public String getMessage( String msg) {
  String result = "Hello : " + msg;
  return result;
 }
}



Step 4: Wire up the above Java artifacts via Spring, by creating the applicationContext.xml file under  src/main/resources/com/mycompany.

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


 <bean id="simpleWebService" class="com.mycompany.SimpleWebServiceImpl">
  <constructor-arg ref="simpleService" />
 </bean>


 <bean id="simpleService" class="com.mycompany.SimpleServiceImpl" />

</beans>


Step 5: Bootstrap the Spring applicationContext.xml file and the RESTEasy config via the web.xml file.

<!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>
 <display-name>Archetype Created Web Application</display-name>

 <!-- this need same with resteasy servlet url-pattern -->
 <context-param>
  <param-name>resteasy.servlet.mapping.prefix</param-name>
  <param-value>/rest</param-value>
 </context-param>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/com/mycompany/applicationContext.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
   org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
  </listener-class>
 </listener>
 <listener>
  <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
 </listener>

 <servlet>
  <servlet-name>resteasy-servlet</servlet-name>
  <servlet-class>
   org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
  </servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>resteasy-servlet</servlet-name>
  <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

</web-app>


Noteresteasy.scan context param has been removed, and  contextConfigLocation has been added to  define the the Spring applicationContext.xml file. A new listener defining "SpringContextLoaderListener" has been added to bootstrap spring in a web application.


Step 6: Build the application using "mvn package" command as previous tutorial, and access the app using the URL  http://localhost:8080/RESTfulWebService/rest/simple/Arul.




Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home