Ryan has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Setting Up JAX-RS in Pre-Java EE 6 Environments

02.02.2010
| 6934 views |
  • submit to reddit

The JAX-RS API for developing RESTful web services is a Java EE 6 API. Java EE 5 application servers and Tomcat don't come with JAX-RS, so you will need to add it into your project. This article shows how to use Sun's Jersey implementation with Maven, and in case you are interested, how to integrate it with Spring.

Start by adding dependencies to your Maven pom.xml:

 <dependencies>  
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.1.4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.1.4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.12</version>
<scope>provided</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>

The Jersey implementation is a servlet, but you can also use it as a servlet filter. I found that using it as a servlet made it either not respond to any requests, or it would break other things like JSF. So, I recommend using the servlet filter approach:

 <filter>  
<filter-name>Jersey Web Application</filter-name>
<filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ryandelaplante.example</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.PostReplaceFilter;com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.logging.DisableEntitylogging</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Jersey Web Application</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>

 

This configures Jersey to respond to /rest/* URIs. It also enables the logging filter so you can see the request and response headers and bodies. The PostReplaceFilter enables support for sending an X-HTTP-Method-Override header in POST requests to translate it into a PUT or DELETE. This is necessary for supporting some RESTful web service clients that don't support PUT or DELETE.

The com.sun.jersey.config.property.packages init-param tells Jersey which package(s) to recursively scan for classes that have JAX-RS annotations. I think you can separate multiple packages with a semicolon.

Your JAX-RS resources are managed by Jersey. You can inject Spring beans into it using the com.sun.jersey.spi.inject.Inject annotation, as long as you have the jersey-spring dependency added to your pom.xml in Maven. For example:

  package com.ryandelaplante.example;  

import com.sun.jersey.spi.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Consumes("text/xml")
@Produces("text/xml")
@Path("/rest/orders")
public class OrdersResource {
@Inject
private OrderService orderService;

...
}


The OrderService will be loaded from the Spring container.

From http://www.ryandelaplante.com

Published at DZone with permission of its author, Ryan Developer.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: