{"id":878,"date":"2015-03-25T15:25:12","date_gmt":"2015-03-25T19:25:12","guid":{"rendered":"http:\/\/shirishranjit.com\/blog1\/?page_id=878"},"modified":"2016-07-29T12:36:42","modified_gmt":"2016-07-29T16:36:42","slug":"rest-with-jersey","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/technical-posts\/webservice-topic\/rest-with-jersey","title":{"rendered":"Step by Step example to build REST Service with JAX-RS"},"content":{"rendered":"<h1>REST service with Java JAX-RS step by step:<\/h1>\n<ol>\n<li>Include following to two mapping in your web.xml<\/li>\n<pre>\r\n&nbsp;\r\n&lt;servlet&gt;\r\n    &lt;servlet-name&gt;javax.ws.rs.core.Application&lt;\/servlet-name&gt;\r\n    &lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n&lt;\/servlet&gt;\r\n\r\n&lt;servlet-mapping&gt;\r\n    &lt;servlet-name&gt;javax.ws.rs.core.Application&lt;\/servlet-name&gt;\r\n    &lt;url-pattern&gt;\/yourAppURL\/*&lt;\/url-pattern&gt;\r\n&lt;\/servlet-mapping&gt;\r\n<\/pre>\n<li>Create a class for REST Service. The @PATH annotation must be defined at the class level not at the method level.<\/li>\n<pre>\r\n\/**\r\n* @author shirish\r\n* @Jul 11, 2016\r\n*\r\n*\/\r\n@Path(\"\/hello\")\r\npublic class HelloWorldClass {\r\n\r\n\/**\r\n*\r\n*\/\r\npublic HelloWorldClass() {\r\n\r\n  }\r\n\r\n@GET\r\n@Produces(\"text\/plain\")\r\npublic String getClichedMessage() {\r\n\r\n    return \"Hello World\";\r\n  }\r\n}\r\n<\/pre>\n<li>Classpath in the war.\u00a0 &#8211;<\/li>\n<p>The easiest way to get war working in a java web container is to put all application classes in <em>\u201cyourWebApp\/WEB-INF\/classes\u201d<\/em> such that you do not need to fix your application classpath to include jars.<br \/>\n<br \/>\nNote: even if you put your application jars in <em>\u201cyourWebApp\/WEB-INF\/lib\u201d<\/em>, the jars may not be in classpath properly; therefore, may not have the classes loaded that are necessary.<\/p>\n<li>Build and Deploy in JETTY or any application server.<\/li>\n<li>path to get to your REST service<\/li>\n<pre>\r\n    http:\/\/localhost:8080\/yourapp\/yourAppURL\/hello\r\n<\/pre>\n<\/ol>\n<h2>Reference:<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/tutorial\/jaxrs002.htm\">Oracle REST Example<\/a><\/li>\n<li><a href=\"http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/gilik.html\">http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/gilik.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.jboss.org\/author\/display\/WFLY10\/Java+API+for+RESTful+Web+Services+(JAX-RS)\">Wildfly Example<\/a><\/li>\n<li><a href=\"https:\/\/docs.jboss.org\/author\/display\/WFLY8\/JAX-RS+Reference+Guide\">JBoss Guide for REST<\/a><\/li>\n<li><a href=\"https:\/\/jersey.java.net\/documentation\/latest\/deployment.html\">Jersey Documentation<\/a><\/li>\n<li><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/tutorial\/jaxrs002.htm\">JAX-RS Documentation<\/a><\/li>\n<li><a href=\"https:\/\/jersey.java.net\/documentation\/latest\/user-guide.html\">Jersey User Guide<\/a><\/li>\n<li><a href=\"https:\/\/jersey.java.net\/documentation\/latest\/client.html\">https:\/\/jersey.java.net\/documentation\/latest\/client.html<\/a><\/li>\n<\/ul>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Step+by+Step+example+to+build+REST+Service+with+JAX-RS\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/technical-posts\/webservice-topic\/rest-with-jersey\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>REST service with Java JAX-RS step by step: Include following to two mapping in your web.xml &nbsp; &lt;servlet&gt; &lt;servlet-name&gt;javax.ws.rs.core.Application&lt;\/servlet-name&gt; &lt;load-on-startup&gt;1&lt;\/load-on-startup&gt; &lt;\/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;javax.ws.rs.core.Application&lt;\/servlet-name&gt; &lt;url-pattern&gt;\/yourAppURL\/*&lt;\/url-pattern&gt; &lt;\/servlet-mapping&gt; Create a class for REST Service. The @PATH annotation must be defined at the class &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/technical-posts\/webservice-topic\/rest-with-jersey\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":755,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-878","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/878"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=878"}],"version-history":[{"count":12,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/878\/revisions"}],"predecessor-version":[{"id":1381,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/878\/revisions\/1381"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/755"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}