I have a Servlet 3.0 web application that uses both Spring and Jersey. I have currently configured it using SpringServlet, configured as a filter in web.xml, and resource classes annotated with both @Path and @Component . Here is a snippet of web.xml:
<filter> <filter-name>jersey-serlvet</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.foo;com.bar</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>jersey-serlvet</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
This setting works, but I really want to get this setting only with annotations - there is no web.xml configuration. My first attempt was to remove the above SpringServlet configuration and create a class that extends Application . Here is a snippet of this:
@ApplicationPath("/*") public class MyApplication extends PackagesResourceConfig { public MyApplication() { super("com.foo;com.bar"); HashMap<String, Object> settings = new HashMap<String, Object>(1); settings.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); this.setPropertiesAndFeatures(settings); } }
This works in that the JAX-RS resources are registered and I can hit them at their urls, but they throw NullPointerExceptions when they try to use their properties with autosave ... it makes sense because I assume that the resources Jersey is now loading and Spring beans is not managed, so there is no auto device.
Despite a fair search, I cannot find a way to load Jersey resources like Spring beans with annotations only. Is there such a way? I really don't want to write a bunch of code for resources to manually extract the Spring context and call the DI if I can help it.
If the annotations just don't work, then I can live with the filter configuration in web.xml, if I can specify the Application class to load instead of the list of packages for scanning. If I can get rid of the list of packages there and just specify an instance of the Application class, then I will be pleased.
Obviously, it would be great if someone had a definitive answer for me, but I would also be grateful for any pointers or hints on where else I could look or try something.
Thanks Matt
Donuts
source share