Creating RESTful Web Service with Maven, Jersey 1.8 and JBoss 7.1.1 Final in Eclipse

A few days back my Team Lead asked me to set up a demo web service with Jersey, Jboss 7.1.1 final and Maven in Eclipse Juno. I wasted 2 days flat to search the tutorial & set up only a hello world application in web service. So, I thought . may be this will be of some help who are going to use jersey with jboss in near future.
So, here is the tutorial with screenshots :
(I am assuming that everyone knows how to set up Maven in eclipse or may be you can refer to this tutorial - Installing Maven in Eclipse Juno or Higher



Step: 1
  a. Install Maven for Eclipse.
  b. Open standalone.xml of JBoss Server & comment the sections for jaxrs
      (search with jaxrs & comment them ;))

Step: 2
   Set the repository for Maven in eclipse
   for this, follow the below steps:

   a. Open Preferences in Eclipse (Eclipse Window -> Preferences)
   b. Type Maven in search box & then, click on User Settings.
   c. Set the path for settings.xml for Maven.
   
Maven Repository Setting : Step 2

   d. Click on Apply.

Step: 3

   Create a new Dynamic Web Project.
   a. File -> New -> Dynamic Web Project.
   b. Give a suitable project name, select Target Runtime as JBoss 7.1 as Runtime & Configuration as Default Configuration for JBoss 7.1 Runtime & click on next.
Creating Dynamic Web Project , Step: 3.b 
c. Click on checkbox: Generate Xml
Step : 3.c
d. Click on Finish.

Step: 4
    Convert the project into Maven Project.

   a. Right click on the project -> configure -> convert to Maven. A box will open prompting to create a pom.xml for the project.
Converting to Maven Project, Step: 4.a

b. Type package name as Group Id & Project Name as Artifact Id & click on Finish. This is very important.

Step: 5

   a. Delete src package from Java Resources.
   b. Create new source folder: src/main/java
Create a source folder, Step : 5.b

   c. The folder structure should now look like this:
Folder Structure

   d. The pom.xml will look like this:
pom.xml
   e. web.xml will look like this:
web.xml
Step: 6

a. Create a Java class TestRest in com.rest.testpackage
b. Develop the following code like this:
Code

c.  paste this code in web.xml
 

<web-appid="WebApp_ID"version="2.4"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/j2ee"xsi:schemalocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Restful Web 

Application</display-name>

<servlet>

<servlet-name>TestServlet</servlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer

</servlet-class>

<!-- 

<init-param>

<param-name>com.sun.jersey.config.property.packages</param-name><param-value>com.rest.test</param-value></init-param> -->

<load-on-startup>4</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>TestServlet</servlet-name>

<url-pattern>/resources/*</url-pattern>

</servlet-mapping>

</web-app>

d. paste this code in pom.xml

<projectxmlns="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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<repositories> 

 

<repository> 

 

<id>maven2-repository.java.net</id> 

 

<name>Java.net Repository for 

Maven</name> 

 

<url>http://download.java.net/maven/2/</url> 

 

<layout>default</layout> 

 

</repository> 

 

</repositories>

<groupId>src.main.java</groupId>

<artifactId>Test</artifactId>

<version>0.0.1-SNAPSHOT</version>

<build>

<sourceDirectory>src</sourceDirectory>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<source>1.6</source>

<target>1.6</target>

</configuration>

</plugin>

</plugins>

</build>

<dependencies> 

 

<dependency>

<groupId>com.sun.jersey</groupId>

<artifactId>jersey-core</artifactId>

<version>1.8</version>

</dependency>

<dependency>

<groupId>com.sun.jersey</groupId>

<artifactId>jersey-server</artifactId>

<version>1.8</version>

</dependency>

</dependencies>

</project>

Step: 7

   a. Make sure that the versions of jars is same as the version mentioned in pom.xml.

   b. Make sure that the versions of jerser-server.jar & jersey-core.jar are same.


Step: 8

   Run Maven as Install . Right Click -> Run As -> Maven Install.

Step: 9

   Run on server or export it to a folder & deploy it manually & run the url:

   http://localhost:8080/Test/resources/test/ABC (your port number may be different)

   The output will be : 
Hello ABC

Well, this is the basic Jersey Web Service which is created using Eclipse Juno, Maven, Jersey 1.8 & JBoss 7.1.1 final . In the next post will discuss how to create a client which will use this webservice .

Feel free to share this & post some response. May be, you can just let me know if it is helpful to you.

Related posts:
Using parameters in URLs in Jersey Web Services
Write a Jersey Web Service that returns XML
A Simple Client for Webservice