RESTful Web Services with Jersey JAX-RS on Tomcat 7

In this tutorial, We are going to show you how to develop RESTful services with Jersey and how to deploy them on a Tomcat server. The RESTful approach of developing web services is constantly gaining more and more attention and seems to be pushing SOAP into deprecation.Now the follow the following steps to build the JAX-RS web service with Jersey and deploy to Tomcat7.

Technologies and Tools used in this article:

  • Jersey 2.0
  • JDK 1.7
  • Tomcat 7.0
  • STS 2.7

Step 1) Create Dynamic Web Application “RESTWebApp“.

RESTful Web Services with Jersey JAX-RS on Tomcat 7

Step 2) Create web.xml (deployment descriptor) under WebRootWEB-INF .

Step 3) Open web.xml file and add below code just above :

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RESTWebApp</display-name>
 <servlet>
  <servlet-name>jersey-serlvet</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>jersey-serlvet</servlet-name>
  <url-pattern>/doj/*</url-pattern>
 </servlet-mapping>
</web-app>

Step 4) Adding two jars file You can download from here: asm-3.3.1.jar, jersey-bundle-1.14.jar

Step 5) Creating Employee bean class
Employee.java

package com.dineshonjava.ws.rest.bean;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Dinesh Rajput
 *
 */

@XmlRootElement(name = "employee")
public class Employee {
 private String employeeId;
    private String employeeName;
    private String jobType;
    private String address;
    private Long salary;
    
    @XmlElement
    public String getEmployeeId() {
        return employeeId;
    }
    
    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    
    @XmlElement
    public String getEmployeeName() {
        return employeeName;
    }
    
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    @XmlElement
 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 @XmlElement
 public Long getSalary() {
  return salary;
 }

 public void setSalary(Long salary) {
  this.salary = salary;
 }

 @XmlElement
 public String getJobType() {
  return jobType;
 }

 public void setJobType(String jobType) {
  this.jobType = jobType;
 }
       
}

Step 6) Create WebController.java file

package com.dineshonjava.ws.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.dineshonjava.ws.rest.bean.Employee;

/**
 * @author Dinesh Rajput
 *
 */

@Path("/webservice")
public class WebController {
 
  private static Map<String, Employee> employees = new HashMap<String, Employee>();
     
     static {
         
         Employee employee1 = new Employee();
         employee1.setEmployeeId("11111");
         employee1.setEmployeeName("Dineh Rajput");
         employee1.setJobType("Sr.Software Engineer");
         employee1.setSalary(70000l);
         employee1.setAddress("Noida");
         employees.put(employee1.getEmployeeId(), employee1);
         
         Employee employee2 = new Employee();
         employee2.setEmployeeId("22222");
         employee2.setEmployeeName("Abhishek");
         employee2.setJobType("Marketing");
         employee2.setSalary(50000l);
         employee2.setAddress("New Delhi");
         employees.put(employee2.getEmployeeId(), employee2);
         
     }

     @GET
     @Path("/hello")
     @Produces("text/plain")
     public String hello(){
         return "Hello World!!! dineshonjava";    
     }
     
     @GET
     @Path("/message/{message}")
     @Produces("text/plain")
     public String showMsg(@PathParam("message") String message){
         return message;    
     }
     
     @GET
     @Path("/employees")
     @Produces("application/xml")
     public List<Employee> listEmployees(){
         return new ArrayList<Employee>(employees.values());
     }
     
     @GET
     @Path("/employee/{employeeid}")
     @Produces("application/xml")
     public Employee getEmployee(@PathParam("employeeid")String employeeId){
         return employees.get(employeeId);        
     }
     
     @GET
     @Path("/json/employees/")
     @Produces("application/json")
     public List<Employee> listEmployeesJSON(){
         return new ArrayList<Employee>(employees.values());
     }

     @GET
     @Path("/json/employee/{employeeid}")
     @Produces("application/json")
     public Employee getEmployeeJSON(@PathParam("employeeid")String employeeId){
         return employees.get(employeeId);        
     }
  
}

Step 7) Deploy project “RESTWebApp” on Tomcat7. Web project should be deployed without any exception.

Step 8) If every thing fine then test it now following link.
http://localhost:8181/sdnext/doj/webservice/hello

RESTful Web Services with Jersey JAX-RS

http://localhost:8181/sdnext/doj/webservice/message/Welcome%20to%20DineshOnJava.com

output-jax-rs3

http://localhost:8181/sdnext/doj/webservice/employees

output-jax-rs4

http://localhost:8181/sdnext/doj/webservice/employee/11111

output-jax-rs5

http://localhost:8181/sdnext/doj/webservice/json/employees

output-jax-rs6

 http://localhost:8181/sdnext/doj/webservice/json/employee/11111

Download Source Code + Libs
RESTWebApp.zip

References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next

14 Comments

  1. Anonymous September 14, 2013
  2. Anonymous October 23, 2013
  3. Ajimon K.S December 2, 2013
  4. Ankit Katiyar December 17, 2013
  5. Akshay Sahu January 3, 2014
  6. Anonymous January 30, 2014
  7. Paweł M February 3, 2014
  8. Goutam February 19, 2014
  9. Goutam February 19, 2014
  10. Goutam February 19, 2014
  11. Anamika February 19, 2014
  12. Gyanendra Singh Mull April 22, 2018
  13. Dinesh Rajput September 24, 2018
  14. Bhupendra February 6, 2019