{"id":63,"date":"2012-10-17T14:56:54","date_gmt":"2012-10-17T14:56:54","guid":{"rendered":"http:\/\/blogs.sussex.ac.uk\/coursedata\/?p=63"},"modified":"2012-10-17T15:13:19","modified_gmt":"2012-10-17T15:13:19","slug":"new-features-of-java-ee-6-used-in-this-project-part-2","status":"publish","type":"post","link":"https:\/\/blogs.sussex.ac.uk\/coursedata\/2012\/10\/17\/new-features-of-java-ee-6-used-in-this-project-part-2\/","title":{"rendered":"New features of Java EE 6 used in this project (part 2)"},"content":{"rendered":"<p>As mentioned in my last post, the XCRI-CAP project has allowed us to use many of the new features of Java EE 6. One of the new features we&#8217;ve been able to implement is the RESTful web service support via JAX-RS.\u00a0 Previously we have implemented JAX-WS web services by turning EJB Session beans into web services. For the XCRI-CAP project, we have implemented a RESTful service using the Jersey implementation of <a href=\"http:\/\/jersey.java.net\/\">JAX-RS<\/a>.<\/p>\n<p>JAX-RS makes it very easy to produce a RESTful web service. The first thing to do is to register the Jersey REST servlet with the application&#8217;s web.xml.<\/p>\n<p><code><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;servlet&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;servlet-name&gt;JerseyRESTService&lt;\/servlet-name&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;\/servlet-class&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;init-param&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;param-name&gt;com.sun.jersey.config.property.packages&lt;\/param-name&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;param-value&gt;uk.ac.susx.xcricap.session&lt;\/param-value&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/init-param&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/servlet&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;servlet-mapping&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;servlet-name&gt;JerseyRESTService&lt;\/servlet-name&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;\/CourseCatalog\/*&lt;\/url-pattern&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/servlet-mapping&gt;<\/code><\/p>\n<p>This Servlet will be used for any request mapped to \/CourseCatalog.<\/p>\n<p>Next, we can create an EJB that will act as the actual REST service. Using the @Path annotation at the top of the Session bean will control which requests will be handled by the class. In this instance, I want all requests to \/CourseCatalog to be handled by this Session bean:<\/p>\n<p>\/**<br \/>\n&nbsp;*&nbsp;REST&nbsp;Web&nbsp;Service<br \/>\n&nbsp;*<br \/>\n&nbsp;*&nbsp;@author&nbsp;rjb41<br \/>\n&nbsp;*\/<br \/>\n<strong>@Path(&#8220;\/&#8221;)<\/strong><br \/>\n@Stateless<br \/>\npublic&nbsp;class&nbsp;XCRI_CAPRestService&nbsp;{<\/p>\n<p>Next we can write a method that will receive the RESTful requests. The following method handles Rest parameters sent to the \/CourseCatalog\/rest\/foo\/bar URL:<\/p>\n<p><code><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\/**<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Returns&nbsp;the&nbsp;REST&nbsp;XML&nbsp;representation&nbsp;of&nbsp;the&nbsp;service&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@todo&nbsp;specify&nbsp;search&nbsp;criteria<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;cId<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;@GET<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;@Path(\"\/rest\/{name}\/{value}\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;@Produces(MediaType.TEXT_XML)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getXCRIXml(@PathParam(\"name\")&nbsp;String&nbsp;name,&nbsp;@PathParam(\"value\")&nbsp;String&nbsp;value)&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!(new&nbsp;File(xcriSession.getSearchCourseFileName())).exists())&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xcriSession.createCoursesFile(true);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;outputDocument(xcriSession.searchCatalog(name,&nbsp;value)).toString();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n<\/code><\/p>\n<p>This method treats the first argument as the parameter name and the second as the value to search on, e.g.<br \/>\nXCRI-CAP\/CourseCatalog\/rest\/subject\/European would return all courses that contain the word European (the application is actually querying the XML for nodes that end in subject and contain the word &#8216;European&#8217;).<\/p>\n<p>Of course we can do more complex URL searches but for the purposes of demonstration, this shows what is possible.<\/p>\n<p>There are plenty of good tutorials to be found on line, here is the official Oracle documentation:<br \/>\n<a href=\"http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/gipzz.html\">http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/gipzz.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned in my last post, the XCRI-CAP project has allowed us to use many of the new features of Java EE 6. One of the new features we&#8217;ve been able to implement is the RESTful web service support via JAX-RS.\u00a0 Previously we have implemented JAX-WS web services by turning EJB Session beans into web [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[169],"tags":[201,202,200,203,204],"_links":{"self":[{"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/posts\/63"}],"collection":[{"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":4,"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/posts\/63\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sussex.ac.uk\/coursedata\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}