Create Simple Java RESTful Web Services Using Jersey

In this tutorial you will learn how to create simple Java RESTful web services using Jersey framework.

JAX-RS is the Java API used for creating RESTful web services. There are mainly two types of implementation of JAX-RS that are Jersey and RESTeasy. In this tutorial we will see Jersey implementation.

We will use eclipse to create our web service. Make sure it contains apache tomcat server. You can follow below link to know how to download and configure tomcat.

Read: Configure Apache Tomcat Server in Eclipse IDE

Create Simple Java RESTful Web Services Using Jersey 1

Also Read: Create Java SOAP Web Service Using Eclipse

How to Create Java RESTful Web Services Using Jersey Framework

1. First of all download the Jersey jars from below link.

https://jersey.java.net/download.html

2. Open eclipse and go to File -> New -> Dynamic Web Project to create a dynamic web project with name JavaRESTfullWS.

3. Now we have to import jersey jars in our project. The file that you have downloaded in previous step is compressed file. After extracting it you will get several folders. Just import all the jars present in those folders. You can follow below link if you don’t know how to import jars in eclipse.

http://stackoverflow.com/questions/4440479/how-can-i-add-jar-files-to-the-web-inf-lib-folder-in-eclipse

4. Create a package with name example under src folder in your project. Under this package create a source file with name DemoService and add following code inside it.

DemoService.java

package example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/DemoService")
public class DemoService {
		
	@GET
	@Path("/{msg}")
	@Produces(MediaType.TEXT_HTML)
	public String printMsg(@PathParam("msg") String msg){
		return "<h1>Your message: "+msg+"</h1>";
	}	
}

 

5. Enter following code in web.xml file. You can find it under Web-Content -> WEB-INF folder. If it is not there then create one.

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"   
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
id="WebApp_ID" version="3.0">  

 <servlet>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>  
        <param-name>jersey.config.server.provider.packages</param-name>  
        <param-value>example</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>  
</web-app>

 

The project has following structure.

Create Simple Java RESTful Web Services Using Jersey 2
6. Right click on your project folder and then select Run As -> Run on Server.

7. For testing the web service open following url in browser.

http://localhost:8080/JavaRESTfullWS/rest/DemoService/Hello

Create Simple Java RESTful Web Services Using Jersey 3

You can change Hello at the end of the url to pass some other message as parameter.

Video Tutorial

You can watch below video. Everything is explained in it step by step.

Comment below if you are facing any difficulty to create Java RESTful web services.

Happy Coding!! 🙂 🙂

4 thoughts on “Create Simple Java RESTful Web Services Using Jersey”

  1. Create a package something like com.tutorial.jersey and add same in web.xml. example is like folder not a pachage. I guess this was the problem and it solved for me

Leave a Comment

Your email address will not be published. Required fields are marked *