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:
1: package com.ryandelaplante.example;
2:
3: import com.sun.jersey.spi.inject.Inject;
4: import javax.ws.rs.Consumes;
5: import javax.ws.rs.Path;
6: import javax.ws.rs.Produces;
7:
8: @Consumes("text/xml")
9: @Produces("text/xml")
10: @Path("/rest/orders")
11: public class OrdersResource {
12: @Inject
13: private OrderService orderService;
14:
15: ...
16: }
The OrderService will be loaded from the Spring container.

3 comments:
Hello Ryan,
I'm a java developer and I've been using Glassfish v2.1 since 2008, using a SQL Server as my database trough jtds Driver. I read your blog often, and I realize that you work with a similar set of tools, so I decided to contact you to check whether you had this problem I'm facing for two weeks with no solution.
Every application that I deploy in my server, even the simple ones, which use jtds and hibernate, can't be undeployed cleanly. I don't even need to run the application to get classes locked in memory (permgen) after undeployment.
I've already tried many things, such as puting jtds, hibernate and its dependencies in glassfish/lib folder, manually unregister the driver trough DriverManager, and many other things with no effect. Have you ever faced such a problem? Do you know whether it is really possible to redeploy applications in Glassfish without restarting it?
I`ve ttried to analyse my heap using jmap and jhat but I'm not being able to locate what is locking my classes in memory.. I've already replaced every static field declared in my application with SoftReference objects, but it didn`t work either. I'm completely lost in this case, any help would be much appreciated.
Thank you,
Marcus
Hi Ryan,
I'm giving up: I have tried to set up your example using the Filter-Based approach. I'm only able to get the example working when I use the 'old' Servlet-Based approach. Could you provide me with a complete example. Could you please send me a .zip with the whole code?
Much thanks in advance!
Best regards,
Christian
Hi Christian,
The examples in this blog entry were copied from a working closed source project, and only the OrdersResource was renamed. Are you using Spring with JAX-RS? If not, then that is probably the problem since I used a Spring specific filter class:
com.sun.jersey.spi.spring.container.servlet.SpringServlet
If you are not using Spring, then use this filter class:
com.sun.jersey.spi.container.servlet.ServletContainer
Also, the jersey-spring dependency in Maven isn't needed if you aren't using Spring.
Let me know if that helps. If it doesn't, then what are the symptoms or errors of the problem?
Thanks,
Ryan
Post a Comment