The purpose of this post is only to provide a jump start in setting up a Maven environment and start working with Jersey . I will try to provide a detailed version later when time permits. The existing tutorials are outdated to some extent due to refactoring. Hence I hope this post is useful for developers beginning to use Jersey.
Note: The IDE used in screen shots is NetBeans 6.5 Beta with Maven plugin and tested with Tomcat 6.0 and GlassFish.
- Select File--> New Project --> Maven --> Maven Project from the menu.
- Click Next button and then select Maven Webapp archetype.
- Click Next button and please fill in relevant details as in the screenshot.

Click Finish button. NetBeans will take some time in downloading the required plugins. So please be patient for a minute or two.
Right click the project and select Properties and choose Source/Binary format as 1.6 by clicking on the Sources category on the left. Else the default Java version is 1.3 in which annotation is not supported.
Now let us enter the repository and dependency information in pom.xml as below. I have provided the content of pom.xml below.
<?xml version="1.0" encoding="UTF-8"?>
<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.thea</groupId>
<artifactId>jerseymavensample</artifactId>
<packaging>war</packaging>
<version>0.1</version>
<name>jerseymavensample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>0.11-ea-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>0.11-ea-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>0.11</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>javamaven2</id>
<name>Repository for Maven2</name>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<build>
<finalName>jerseymavensample</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<netbeans.hint.deploy.server>Tomcat60</netbeans.hint.deploy.server>
</properties>
</project>
Also we will modify web.xml to include Jersey servlet, so that it processes related requests.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Now let us create a Java class StudentDetails.java which will be the REST implementation front end. In real life applications this will retrieve the details from database.
package com.ts.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
*
* @author karthikeyanc
*/
@Path("/studentdetails")
public class StudentDetails {
@GET
@Produces("text/plain")
public String getStudentDetails() {
return "Student details: Year of graduation :2001 College Name: CEG";
}
}
Now you can select a server to deploy by right clicking the project and choosing properties--> Run.option.
Then right click the Project and Run the application. To test use the following URL (modify the port number and the context root as required)
http://localhost:8090/jerseymavensample/studentdetails
The result is as shown in the screenshot below.

Now let us modify the above StudentDetails.java to retrieve form parameters and process them. Here we retrieve a HTTP input from the HttpRequest and uses the same in response. The modified Java file is as below.
package com.ts.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
*
* @author karthikeyanc
*/
@Path("/studentdetails")
public class StudentDetails {
@POST
@Produces("text/plain")
@Consumes("application/x-www-form-urlencoded")
public String getStudentDetails(@FormParam("name") String studentName) {
return "Student details: Name:"+studentName+" Year of graduation :2001 College Name: CEG";
}
}
We can make use of the below HTML file to invoke the above.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="http://localhost:8090/jerseymavensample/studentdetails" method=POST>
<input type=text name="name" value="MSA"/>
<input type=submit value="click"/>
</form>
</body>
</html>
The output is as below in the browser.
Student details: Name:MSA Year of graduation :2001 College Name: CEG
