Deploying Jersey to JBOSS
I spent the afternoon playing around with Jersey in JBOSS. Jersey is a Java API for publishing REST web-services, and JBOSS is the application server we use at work. Not finding any particularly helpful tutorials, I muddled my way through. Lucky for you, I have distilled the required steps here.
The first step is to create a Java class with the method you would like to invoke via a REST call. Here is what I came up with:
package com.bitkickers; import java.util.Map; import javax.ws.rs.*; @Path("JerseyServices") public class JerseyServices { @GET @Produces({"text/plain"}) @Path("/helloWorld") public String helloWorld(@QueryParam("name") String name) { return String.format("Hello, %s", name); } }
The annotations include @Path("JerseyServices"), which defines a path relative to the web context to serve all of the methods in this class from. @GET, tells Jersey to attach to HTTP GET requests for this method. @Path("/helloWorld") and @QueryParam("name") combine to define the whole relative URL as "/JerseyServices/helloWorld?name=Chase" to call this method with the parameter name set to "Chase".
The web.xml to tie in the Jersey servlet looks like the following:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Jersey API Skunkworks</display-name> <servlet> <servlet-name>JerseyAPI</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.bitkickers</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>JerseyAPI</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> </web-app>
The parameter "com.sun.jersey.config.property.packages" tells "ServletContainer" to scan for any classes annotated with @Path in the package "com.bitkickers", recursively. The servlet mapping tells the servlet to attach to the URL "/api".
Finally, the jboss-web.xml just sets the root context to "jersey":
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.4//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd"> <jboss-web> <context-root>jersey</context-root> </jboss-web>
All together, the URL to invoke this REST call becomes "http://localhost:8080/jersey/api/JerseyServices/helloWorld?name=Chase".
Also included in the download is an ANT file to build the WAR, and an HTML file that uses jQuery to invoke the REST service.