RESTful Web Services with Jersey JAX-RS on Tomcat 7

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">In this tutorial&comma; We are going to show you how to develop <b>RESTful <&sol;b>services with <b>Jersey <&sol;b>and how to deploy them on a <b>Tomcat <&sol;b>server&period; The <b>RESTful <&sol;b>approach of developing web services is constantly gaining more and more attention and seems to be pushing <b><a class&equals;"ext-link" title&equals;"" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-ws-web-service-tutorial&sol;" target&equals;"&lowbar;blank" rel&equals;"external noopener">SOAP<&sol;a><&sol;b> into deprecation&period;Now the follow the following steps to build the <b>JAX-RS<&sol;b> web service with <b>Jersey <&sol;b>and deploy to <b>Tomcat7<&sol;b>&period;<&sol;p>&NewLine;<h2><b>Technologies and Tools used in this article&colon;<&sol;b><&sol;h2>&NewLine;<ul>&NewLine;<li>Jersey 2&period;0<&sol;li>&NewLine;<li>JDK 1&period;7<&sol;li>&NewLine;<li>Tomcat 7&period;0<&sol;li>&NewLine;<li>STS 2&period;7<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Step 1&rpar; <&sol;b>Create Dynamic Web Application &&num;8220&semi;<i><b>RESTWebApp<&sol;b><&sol;i>&&num;8220&semi;&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;jax-rs-directory2&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Step 2&rpar; <&sol;b>Create <b>web&period;xml<&sol;b> &lpar;deployment descriptor&rpar; under <i><b>WebRootWEB-INF <&sol;b><&sol;i>&period;<&sol;p>&NewLine;<p><b>Step 3&rpar;<&sol;b> Open <b>web&period;xml <&sol;b>file and add below code just above &colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;web-app xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee" xmlns&colon;web&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;2&lowbar;5&period;xsd" xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;3&lowbar;0&period;xsd" id&equals;"WebApp&lowbar;ID" version&equals;"3&period;0"&gt&semi; &NewLine; &lt&semi;display-name&gt&semi;RESTWebApp&lt&semi;&sol;display-name&gt&semi; &NewLine; &lt&semi;servlet&gt&semi; &NewLine; &lt&semi;servlet-name&gt&semi;jersey-serlvet&lt&semi;&sol;servlet-name&gt&semi; &NewLine; &lt&semi;servlet-class&gt&semi; &NewLine; com&period;sun&period;jersey&period;spi&period;container&period;servlet&period;ServletContainer &NewLine; &lt&semi;&sol;servlet-class&gt&semi; &NewLine; &lt&semi;load-on-startup&gt&semi;1&lt&semi;&sol;load-on-startup&gt&semi; &NewLine; &lt&semi;&sol;servlet&gt&semi; &NewLine; &NewLine; &lt&semi;servlet-mapping&gt&semi; &NewLine; &lt&semi;servlet-name&gt&semi;jersey-serlvet&lt&semi;&sol;servlet-name&gt&semi; &NewLine; &lt&semi;url-pattern&gt&semi;&sol;doj&sol;&ast;&lt&semi;&sol;url-pattern&gt&semi; &NewLine; &lt&semi;&sol;servlet-mapping&gt&semi; &NewLine;&lt&semi;&sol;web-app&gt&semi;<&sol;pre>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>Step 4&rpar;<&sol;b> Adding two jars file You can download from here&colon; <b>asm-3&period;3&period;1&period;jar&comma; jersey-bundle-1&period;14&period;jar <&sol;b><&sol;p>&NewLine;<p><b>Step 5&rpar;<&sol;b> Creating Employee bean class<br &sol;>&NewLine;<b>Employee&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;ws&period;rest&period;bean&semi; &NewLine; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlElement&semi; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlRootElement&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine; &NewLine;&commat;XmlRootElement&lpar;name &equals; "employee"&rpar; &NewLine;public class Employee &lbrace; &NewLine; private String employeeId&semi; &NewLine; private String employeeName&semi; &NewLine; private String jobType&semi; &NewLine; private String address&semi; &NewLine; private Long salary&semi; &NewLine; &NewLine; &commat;XmlElement &NewLine; public String getEmployeeId&lpar;&rpar; &lbrace; &NewLine; return employeeId&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setEmployeeId&lpar;String employeeId&rpar; &lbrace; &NewLine; this&period;employeeId &equals; employeeId&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;XmlElement &NewLine; public String getEmployeeName&lpar;&rpar; &lbrace; &NewLine; return employeeName&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setEmployeeName&lpar;String employeeName&rpar; &lbrace; &NewLine; this&period;employeeName &equals; employeeName&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;XmlElement &NewLine; public String getAddress&lpar;&rpar; &lbrace; &NewLine; return address&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setAddress&lpar;String address&rpar; &lbrace; &NewLine; this&period;address &equals; address&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;XmlElement &NewLine; public Long getSalary&lpar;&rpar; &lbrace; &NewLine; return salary&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setSalary&lpar;Long salary&rpar; &lbrace; &NewLine; this&period;salary &equals; salary&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;XmlElement &NewLine; public String getJobType&lpar;&rpar; &lbrace; &NewLine; return jobType&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setJobType&lpar;String jobType&rpar; &lbrace; &NewLine; this&period;jobType &equals; jobType&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>Step 6&rpar; Create <i>WebController&period;java <&sol;i>file<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;ws&period;rest&semi; &NewLine; &NewLine;import java&period;util&period;ArrayList&semi; &NewLine;import java&period;util&period;HashMap&semi; &NewLine;import java&period;util&period;List&semi; &NewLine;import java&period;util&period;Map&semi; &NewLine; &NewLine;import javax&period;ws&period;rs&period;GET&semi; &NewLine;import javax&period;ws&period;rs&period;Path&semi; &NewLine;import javax&period;ws&period;rs&period;PathParam&semi; &NewLine;import javax&period;ws&period;rs&period;Produces&semi; &NewLine; &NewLine;import com&period;dineshonjava&period;ws&period;rest&period;bean&period;Employee&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine; &NewLine;&commat;Path&lpar;"&sol;webservice"&rpar; &NewLine;public class WebController &lbrace; &NewLine; &NewLine; private static Map&lt&semi;String&comma; Employee&gt&semi; employees &equals; new HashMap&lt&semi;String&comma; Employee&gt&semi;&lpar;&rpar;&semi; &NewLine; &NewLine; static &lbrace; &NewLine; &NewLine; Employee employee1 &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee1&period;setEmployeeId&lpar;"11111"&rpar;&semi; &NewLine; employee1&period;setEmployeeName&lpar;"Dineh Rajput"&rpar;&semi; &NewLine; employee1&period;setJobType&lpar;"Sr&period;Software Engineer"&rpar;&semi; &NewLine; employee1&period;setSalary&lpar;70000l&rpar;&semi; &NewLine; employee1&period;setAddress&lpar;"Noida"&rpar;&semi; &NewLine; employees&period;put&lpar;employee1&period;getEmployeeId&lpar;&rpar;&comma; employee1&rpar;&semi; &NewLine; &NewLine; Employee employee2 &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee2&period;setEmployeeId&lpar;"22222"&rpar;&semi; &NewLine; employee2&period;setEmployeeName&lpar;"Abhishek"&rpar;&semi; &NewLine; employee2&period;setJobType&lpar;"Marketing"&rpar;&semi; &NewLine; employee2&period;setSalary&lpar;50000l&rpar;&semi; &NewLine; employee2&period;setAddress&lpar;"New Delhi"&rpar;&semi; &NewLine; employees&period;put&lpar;employee2&period;getEmployeeId&lpar;&rpar;&comma; employee2&rpar;&semi; &NewLine; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;hello"&rpar; &NewLine; &commat;Produces&lpar;"text&sol;plain"&rpar; &NewLine; public String hello&lpar;&rpar;&lbrace; &NewLine; return "Hello World&excl;&excl;&excl; dineshonjava"&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;message&sol;&lbrace;message&rcub;"&rpar; &NewLine; &commat;Produces&lpar;"text&sol;plain"&rpar; &NewLine; public String showMsg&lpar;&commat;PathParam&lpar;"message"&rpar; String message&rpar;&lbrace; &NewLine; return message&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;employees"&rpar; &NewLine; &commat;Produces&lpar;"application&sol;xml"&rpar; &NewLine; public List&lt&semi;Employee&gt&semi; listEmployees&lpar;&rpar;&lbrace; &NewLine; return new ArrayList&lt&semi;Employee&gt&semi;&lpar;employees&period;values&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;employee&sol;&lbrace;employeeid&rcub;"&rpar; &NewLine; &commat;Produces&lpar;"application&sol;xml"&rpar; &NewLine; public Employee getEmployee&lpar;&commat;PathParam&lpar;"employeeid"&rpar;String employeeId&rpar;&lbrace; &NewLine; return employees&period;get&lpar;employeeId&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;json&sol;employees&sol;"&rpar; &NewLine; &commat;Produces&lpar;"application&sol;json"&rpar; &NewLine; public List&lt&semi;Employee&gt&semi; listEmployeesJSON&lpar;&rpar;&lbrace; &NewLine; return new ArrayList&lt&semi;Employee&gt&semi;&lpar;employees&period;values&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;json&sol;employee&sol;&lbrace;employeeid&rcub;"&rpar; &NewLine; &commat;Produces&lpar;"application&sol;json"&rpar; &NewLine; public Employee getEmployeeJSON&lpar;&commat;PathParam&lpar;"employeeid"&rpar;String employeeId&rpar;&lbrace; &NewLine; return employees&period;get&lpar;employeeId&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Step 7&rpar;<&sol;b> Deploy project &&num;8220&semi;<b>RESTWebApp<&sol;b>&&num;8221&semi; on <b>Tomcat7<&sol;b>&period; Web project should be deployed without any exception&period;<&sol;p>&NewLine;<p><b>Step 8&rpar;<&sol;b> If every thing fine then test it now following link&period;<br &sol;>&NewLine;<b><i>http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;webservice&sol;hello<&sol;i><&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;output-jax-rs2&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><i><b>http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;webservice&sol;message&sol;Welcome&percnt;20to&percnt;20DineshOnJava&period;com<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;output-jax-rs3&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><i><b>http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;webservice&sol;employees<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;output-jax-rs4&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><i><b>http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;webservice&sol;employee&sol;11111<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;output-jax-rs5&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><i><b>http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;webservice&sol;json&sol;employees<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;output-jax-rs6&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><i><b> http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;webservice&sol;json&sol;employee&sol;11111<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;output-jax-rs7&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Download Source Code &plus; Libs<&sol;b><br &sol;>&NewLine;<a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;RESTWebApp&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener"><b>RESTWebApp&period;zip<&sol;b><&sol;a><&sol;h2>&NewLine;<p><i><b>References<&sol;b><&sol;i><br &sol;>&NewLine;1&period;<b> <i><a href&equals;"http&colon;&sol;&sol;docs&period;oracle&period;com&sol;javaee&sol;6&sol;tutorial&sol;doc&sol;giepu&period;html" target&equals;"&lowbar;blank" rel&equals;"noopener">JAVA REST Web Services<&sol;a><&sol;i><&sol;b><br &sol;>&NewLine;2&period; <i><b><a href&equals;"http&colon;&sol;&sol;en&period;wikipedia&period;org&sol;wiki&sol;Web&lowbar;service" target&equals;"&lowbar;blank" rel&equals;"noopener">Wikipedia for REST Web Service<&sol;a><&sol;b><&sol;i><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-jersey-hello-world-example&sol;">Previous <&sol;a>&lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;create-restful-java-client-with-jersey&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-jersey-hello-world-example&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;create-restful-java-client-with-jersey&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '430'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago