In this post, I will show how to develop REST Web Service using JAX-RS 2.0 API to run it with Tompcat 7.0 without Web.xml entries of dispatcher servlet..
There are many examples around the web which shows how to develop service with JAX-RS 2.0 Jersey Implementation. So nothing new about it. But most of them use Web.xml entries for Jersey Servlet Dispatcher like below.
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> </servlet>
In web container like Tomcat 7 which support Servlet 3.0 or application server like Glassfish which also support Servlet 3.0 does not need web.xml entries for this dispatcher servlet. Running in Glassfish is more easy because you don't even have to worry about placing Jersey jars with your application as Glassfish has bundled reference implementation of JAR-RS 2.0. When it comes to Tomcat you explicitly have to place Jersey jars in your application classpath. I spent some time to figure out all this and so decided to post entry which might help others.
With this brief background, let me start details.
Developing REST service with JAX-RS annotations is quite easy. I created simple web application with Maven with dependencies of Jersey. Either you use ANT or Maven it is important to make sure that these Jersey libraries get bundled in final deployment WAR. I used Jettison to prepare JSON object. To be very clear, Jettison has absolutely nothing to do with REST service working on Tomcat.
If you are running your application on Glassfish like application server, it is not necessary to have these dependencies. You can just use javax.ws.rs-api-2.0.jar as provided type dependency in that case.
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.11</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.3</version> </dependency>
I have simple POJO annotated with JAX-RS annotations to expose REST API.
@Path("/customerService") public class CustomerService { /** * @param args */ @GET @Path("{id}") @Produces( { MediaType.TEXT_PLAIN }) public String getCustomer(@PathParam("id") String customerId) { try { JSONObject customer = new JSONObject(); JSONObject customerDetails = new JSONObject(); customerDetails.put("id", "123456"); customerDetails.put("firstName", "John"); customerDetails.put("city", "Winsfield"); customer.put("customer", customerDetails); return customer.toString(); } catch (JSONException e) { Logger.getLogger("customerService").log(Level.SEVERE, "Error during JSON Object creation"); } return null; } }
Normally we need to make entry of Jsersy servelet in web.xml. Thanks to Servlet 3.0 specifications this is no more mandatory. What we need to do is
- Create custom class extending javax.ws.rs.core.Application
- Override public Set<Class<?>> getClasses()
- Add service class to set of classes exposed as Service.
@ApplicationPath("/restService/") public class RestServiceApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<Class<?>>(); classes.add(CustomerService.class); return classes; } }
At this point, if we build the application and deploy it on Tomcat 7, REST API will get up and running. We can test it from Internet Browser or favorite utilities like curl , wget.
Invoking REST API URL from browser will return results as JSON string.
That is it. I found some issues with MediaType as JSON with Tomcat which I'm working on and share my experiments with them in coming posts.
No comments:
Post a Comment