Home » RESTful Webservices » JAX-RS & Jersey Example using Maven and Eclipse

JAX-RS & Jersey Example using Maven and Eclipse

Jersey is the open source reference implementation of Java JAX-RS specification. It provides a Java library using which we can easily create RESTful web services in Java platform. JAX-RS / Jersey supports JAXB based XML bindings.

Technologies and Tools used

  • Jersey 1.9
  • Tomcat
  • Eclipse
  • JDK 1.8

Follow the steps mentioned below to develop Hello World program using Jersey and JAX-RS web service.

Directory Structure :


JAX-RS example


Step 1 : Create Web Application Project using Maven Template

Create a Java web application project from maven-archetype-webapp template.

mvn archetype:generate -DgroupId=com.jwt.rest -DartifactId=RESTfullApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Step 2 : Eclipse IDE integration

Convert this project to Eclipse web project with Maven command "mvn eclipse:eclipse -Dwtpversion=1.5".Open command prompt and navigate to generated "RESTfullApp" project and issue following command.

mvn eclipse:eclipse -Dwtpversion=1.5

Step 3 : Import the Project into eclipse

Import the project into eclipse IDE.


Step 4 : Update pom.xml file

Add the jersey dependencies in pom.xml.

pom.xml


<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.jwt.rest</groupId>
	<artifactId>RESTfullApp</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>RESTfullApp Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.17</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-servlet</artifactId>
			<version>1.17</version>
		</dependency>
		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>jsr311-api</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>1.17</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>RESTfullApp</finalName>
	</build>
</project>

After updating the pom.xml again execute "mvn eclipse:eclipse -Dwtpversion=1.5". After executing this command Maven will download required libraries.

Note:- After above step refresh your project otherwise eclipse will not recognize the downloaded files.


Step 5 : Create Java directory :

Create a directory named java under main."/src/main". Right click on the project then select New ->Folder and provide the name as "java" as shown below.


JAX-RS example

Now add this directory to class path of your project.


Step 6 : Add java directory to classpath of the Project :

  • Right click on Project -> Build Path -> Configure build path , a new screen will open in that screen click on Source tab and then click on Add folder as shown bellow.

  • JAX-RS example


  • Again one new screen will open and in that screen select the java checkbox and click on OK button as shown bellow.

  • JAX-RS example

    Now Java folder is in classpath of your project.


Step 7 : Create REST Service :

Create a java class "HelloWorldService" in a package com.jwt.rest and add following code into this.

HelloWorldService.java


package com.jwt.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
 
@Path("/hello")
public class HelloWorldService {
 
	@GET
	@Path("/{name}")
	public Response getMsg(@PathParam("name") String name) {
 
		String output = "Welcome   : " + name;
 
		return Response.status(200).entity(output).build();
 
	}
 
}

Step 8 . Create Web.xml configuration File

Specify Jersey framework Servlet for our application in web.xml file.In web.xml, register "com.sun.jersey.spi.container.
servlet.ServletContainer", and puts your Jersey service folder under "init-param","com.sun.jersey.config
.property.packages".

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>RESTfulExample</display-name>
 <servlet>
		<servlet-name>jersey-serlvet</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.jwt.rest</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

</web-app>

Run the application :

Right click on project -> run as -> run on server Select Apache tomcat and click on finish as shown below.


JAX-RS example


By default eclipse will open http://localhost:8080
/RESTfullApp/. In web.xml we have specified URL pattern as /rest/* (line number 22) and in HelloWorldService.java we specified class level @path as /hello [ line number 8 ] and method level @path as {name} [ line number 1 ],So the final URL should be "http://localhost:8080
/RESTfullApp/rest/hello/java".

Test your REST service with URl "http://localhost:8080/
RESTfullApp/rest/ hello/java"
.You will get following output:

JAX-RS example



Previous Next Article

comments powered by Disqus