<div dir="ltr" style="text-align: justify;">In this tutorial, We are going to show you how to develop <b>RESTful </b>services with <b>Jersey </b>and how to deploy them on a <b>Tomcat </b>server. The <b>RESTful </b>approach of developing web services is constantly gaining more and more attention and seems to be pushing <b><a class="ext-link" title="" href="https://dineshonjava.com/jax-ws-web-service-tutorial/" target="_blank" rel="external noopener">SOAP</a></b> into deprecation.Now the follow the following steps to build the <b>JAX-RS</b> web service with <b>Jersey </b>and deploy to <b>Tomcat7</b>.</p>
<h2><b>Technologies and Tools used in this article:</b></h2>
<ul>
<li>Jersey 2.0</li>
<li>JDK 1.7</li>
<li>Tomcat 7.0</li>
<li>STS 2.7</li>
</ul>
<p><b>Step 1) </b>Create Dynamic Web Application &#8220;<i><b>RESTWebApp</b></i>&#8220;.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/jax-rs-directory2.png" border="0" /></div>
<p><b>Step 2) </b>Create <b>web.xml</b> (deployment descriptor) under <i><b>WebRootWEB-INF </b></i>.</p>
<p><b>Step 3)</b> Open <b>web.xml </b>file and add below code just above :</p>
<pre class="highlight"><;?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>;</pre>
<div id="ads-id" align="center"></div>
<p><b>Step 4)</b> Adding two jars file You can download from here: <b>asm-3.3.1.jar, jersey-bundle-1.14.jar </b></p>
<p><b>Step 5)</b> Creating Employee bean class<br />
<b>Employee.java</b></p>
<pre class="highlight">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; 
 } 
 
} 
 
</pre>
<p><b>Step 6) Create <i>WebController.java </i>file</b></p>
<pre class="highlight">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); 
 } 
 
} 
</pre>
<p><b>Step 7)</b> Deploy project &#8220;<b>RESTWebApp</b>&#8221; on <b>Tomcat7</b>. Web project should be deployed without any exception.</p>
<p><b>Step 8)</b> If every thing fine then test it now following link.<br />
<b><i>http://localhost:8181/sdnext/doj/webservice/hello</i></b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/output-jax-rs2.png" border="0" /></div>
<p><i><b>http://localhost:8181/sdnext/doj/webservice/message/Welcome%20to%20DineshOnJava.com</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/output-jax-rs3.png" border="0" /></div>
<p><i><b>http://localhost:8181/sdnext/doj/webservice/employees</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/output-jax-rs4.png" border="0" /></div>
<p><i><b>http://localhost:8181/sdnext/doj/webservice/employee/11111</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/output-jax-rs5.png" border="0" /></div>
<p><i><b>http://localhost:8181/sdnext/doj/webservice/json/employees</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/output-jax-rs6.png" border="0" /></div>
<p><i><b> http://localhost:8181/sdnext/doj/webservice/json/employee/11111</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/output-jax-rs7.png" border="0" /></div>
<h2><b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/RESTWebApp.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><b>RESTWebApp.zip</b></a></h2>
<p><i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank" rel="noopener">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank" rel="noopener">Wikipedia for REST Web Service</a></b></i></p>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/jax-rs-jersey-hello-world-example/">Previous </a><;<; || <a href="https://dineshonjava.com/jax-rs-web-service-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/create-restful-java-client-with-jersey/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jax-rs-jersey-hello-world-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/create-restful-java-client-with-jersey/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '430'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…