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



11 comments :

  1. really helpful ...keep posting more tutorial :)

    ReplyDelete
  2. Brick wall at Step two for beginners. I was able to install Maven because you provided a link but lost me at "Open standalone.xml of JBoss Server & comment the sections for jaxrs" Could you please elaborate? Does eclipse come with JBoss? Where can this file typically be found?

    ReplyDelete
    Replies
    1. Hi DragonMan,
      Eclipse doesn't come with JBoss! Eclipse is tool for developing Java or J2EE where JBoss is a webserver. You need to download Eclipse & JBoss separately. And, after you download JBoss, you will find 'standalone.xml' in the folder (path - standalone\configuration\standalone.xml in JBoss parent folder).

      Delete
  3. Hi Ritu,

    Im using JDK 7 and tom cat server to publish. On giving the above dependencies, my server does not publish, and throws a WARNING: Exception configuring digester to permit java encoding names in XML files. Only IANA encoding names will be supported.
    org.xml.sax.SAXNotRecognizedException exception.

    If i upgrade the version of jersey server to 1.18, i do not get the exception, but on running the application it says that the rest service was not found. Can you please help.

    Thanks,
    Kinnary

    ReplyDelete
    Replies
    1. Hi Kinnary,

      Please note that this tutorial is for Jersey and Jboss server. There may be some additional configurations required for Tomcat. Please try it with jboss server 7.1.1 final or upper.
      Meanwhile you can send me the full error log you got while running in Tomcat. I can do some RnD n get back to you :) .
      One more thing, it is not at all a good idea to use older version of jersey e.g 1.18.

      Thanks,

      Srijani

      Delete
  4. Hi,

    Your with name "com.sun.jersey.config.property.packages" is commented out in web.xml. Why is it so?

    ReplyDelete
    Replies
    1. Hi,

      This initparameter is something we use when we need some parameter to be initialized when we start the application. But, here for this simple piece of code, I didn't need that configuration, hence, I removed that.

      Thanks,
      Srijani

      Delete
  5. If you want to have an amazing service of Website development in Punjab then Flymedia Technology would be a perfect choice for you to make. We are having an amazing record of successful websites development service.

    ReplyDelete
  6. Nice article.thanks for sharing good post . this is good resource for jersey tutorials

    jersey tutorials

    ReplyDelete
  7. i am so greatful for this article. i also want to suggest digital marketing course as the best career option

    ReplyDelete