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="" xmlns:xsi=""
 <name>RESTfulWebService Maven Webapp</name>

        <!--  JAX-RS dependencies -->




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;


public class SimpleService {
 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.

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

 <display-name>Archetype Created Web Application</display-name>

 <!-- Auto scan REST service -->

 <!-- this need same with resteasy servlet url-pattern -->





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


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


Blogger kiran reddy 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