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: , ,

May 7, 2014

RESTEasy tutorial to create RESTFul web services with Maven and Eclipse

Assumes that you have set up Java and Maven.

Step 1: Generate a maven Web project.

mvn archetype:generate -DgroupId=com.mycompany -DartifactId=RESTfulWebService
 -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


If you set -DinteractiveMode=true, you will be prompted for some inputs.

From the folder where you ran this, you should now have the following file structure created.




Step 2: Open eclipse and import this project in with File --> Import --> Existing Maven Projects, and on the pop-up select the folder RESTfulWebService you created earlier containing the pom.xml file.

Step 3: Open the pom.xml file and add the RESTEasy library 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>
 
 <properties>
      <resteasy.version>2.3.6.Final</resteasy.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>

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

</project>


Step 4: Run the mvn eclipse command to bring in the jar files

C:\Users\akumaras\workspace\RESTfulWebService>mvn eclipse:eclipse

Now, if you go back to eclipse and refresh the project, you will see all the dependency libraries (i.e. jars) added to your build path.



Step 5: Within eclipse create a new source folder src/main/java by right-clicking on the project RESTfulWebService and then selecting Build Path --> New Source Folder ... on the pop up context menu. The folder name is src/main/java.



Step 6: Within src/main/folder right click and create New --> Package. The package name is com.mycompany.

Step 7: Right click on com.mycompany, and  New --> Class.

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 class SimpleService {
 
 @GET
 @Path("/{param}")
 public Response printMessage(@PathParam("param") String msg) {
 
  String result = "Hello : " + msg;
  return Response.status(200).entity(result).build();
 }
}


Step 8: Define the web.xml file. The web deployment descriptor.

<!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>

 <!-- Auto scan REST service -->
 <context-param>
  <param-name>resteasy.scan</param-name>
  <param-value>true</param-value>
 </context-param>

 <!-- 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>

 <listener>
  <listener-class>
   org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
  </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>


Step 9: Package it as a war file using the maven command.

C:\Users\akumaras\workspace\RESTfulWebService>mvn  package


The packaged war file RESTfulWebService.war will look like



Step 10: Deploy the RESTfulWebService.war to an application server, I deployed mine to a JBoss server.

The URL to to try on a web browser is

http://localhost:8080/RESTfulWebService/rest/simple/Arul


where RESTfulWebService is the name of the war file "rest" is configured in the web.xml file as the servlet mapping "/rest/*". "simple" is the path annotation on the SimpleService class @Path("/simple"), and finally, "Arul" is the "{param}" in @Path("/{param}")


Labels: , ,

Apr 1, 2014

RESTEasy web service tutorial basic

Step 1: Create a maven project structure with the following archetype maven command. This assumes that Java and Maven are set up as per the previous tutorials.



mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleRestWeb -DarchetypeArtifactId=maven-archetype-webapp

Step 2: The above command creates a basic Java web structure.



Step 3: Import this into eclipse IDE. File --> Import



Step 4: Click "Next" and browse the folder simpleRestWeb you just created via mvn archetype plugin.


Step 5: Create the "java" source folder by right mouse clicking on simpleRestWeb folder, and then "Build Path" --> "New Souse Folder". type "src/main/java" as the folder name.

Step 6: Update the pom.xml file with the RESEasy library 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.mytutorial</groupId>
 <artifactId>simpleRestWeb</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>simpleRestWeb Maven Webapp</name>
 <url>http://maven.apache.org</url>
 
 <properties>
  <resteasy.version>2.3.6.Final</resteasy.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>

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

 </dependencies>
 <build>
  <finalName>simpleRestWeb</finalName>
 </build>
</project>


Step 7: Define 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>

 <context-param>
  <param-name>resteasy.scan</param-name>
  <param-value>true</param-value>
 </context-param>

 <context-param>
  <param-name>resteasy.servlet.mapping.prefix</param-name>
  <param-value>/rest</param-value>
 </context-param>


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

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


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


This indicates that relative path will be something like /rest/*

Step 8: If you are deploying to a jboss container, define  jboss-web.xml. Otherwise the context root will be simpleRestWeb.

<jboss-web>
   <context-root>tutorial</context-root>
</jboss-web>

Step 9: Define a simple RESTful web  service interface.

package com.mytutorial;

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

@Path("/myapp")
public interface SimpleRestWeb {

 @GET
 @Path("/name/{name}")
 public String sayHello(@PathParam("name") String name);

}

Step 10: The web service implementation

package com.mytutorial;

public class SimpleRestWebImpl implements SimpleRestWeb {

 @Override
 public String sayHello(String name) {
  String result = "Hello " + name;
  return result;
 }

}

Step 11:  Execute mvn clean install from a DOS prompt to build the war file.

Step 12: Deploy the war file to JBoss or any other server.

Step 13:  The url to test

JBoss server with jboss-web.xml

http://localhost:8080/tutorial/rest/myapp/name/arul

The output is: Hello arul

If deployed to any other application server without the  jboss-web.xml

http://localhost:8080/simpleRestWeb/rest/myapp/name/arul






Labels: , ,