Google

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

1 Comments:

Blogger Unknown said...

This comment has been removed by a blog administrator.

11:49 PM, October 08, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home