users@jersey.java.net

[Jersey] Re: Can't Get jersey-spring3 Bridge To Work

From: Owen Jacobson <owen.jacobson_at_grimoire.ca>
Date: Mon, 30 Dec 2013 18:53:38 -0500

On Dec 30, 2013, at 18:38, Michael Iles <michael.iles_at_gmail.com> wrote:

> The sample app referenced in the docs
> (https://github.com/jersey/jersey/tree/2.5/examples/helloworld-spring-webapp)
> defines a Jersey application subclass
> oforg.glassfish.jersey.server.ResourceConfig and programmatically
> registers resources. I was hoping to use a Spring XML config file to
> bind resources, rather than hardcoding them. (This used to work in
> Spring 1.)

I’m in the middle of migrating a Jersey 1 + Spring app to Jersey 2 and ran into this as well. As near as I can tell from inspecting the code, there’s _no way_ in Jersey 2 to automatically detect all JAX-RS resources that are present in the Spring context: you have to arrange for Jersey to detect the presence of JAX-RS resource classes/objects the normal way, even though the actual provisioning of those objects will be done through Spring.

I resorted to putting all of my JAX-RS-annotated objects in a com.example.web package and having Jersey scan that:

> package com.example.web;
>
> import org.glassfish.jersey.server.ResourceConfig;
> import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
>
> public class Application extends ResourceConfig {
> public Application() {
> register(JspMvcFeature.class);
> packages(“com.example.web");
> }
> }

All of the classes in that package are annotated with both @Component and @Path, and Spring handles the wiring.

-o