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“.

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

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

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

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

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

 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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago