users@jersey.java.net

[Jersey] [JERSEY2.2] Injecting a Spring beans in Jersey resources / exception manager

From: <marceloverdijk_at_gmail.com>
Date: Thu, 22 Aug 2013 06:58:47 +0000 (UTC)

I'm using the jersey-spring3 module to integrate Jersey and Spring.
My setup is that we wire up beans using Java config "by hand".
What I mean with "by hand" is that we are not using @Autowired, @Inject
or @Resource annotations.

E.g.:

    @Bean
    public ExceptionMapper<?> exceptionMapper() {
        return new MyExceptionMapper(responseManager());
    }

    @Bean
    public MyService myService() {
        return new MyService();
    }

Now when I have a MyExceptionMapper like:

public class MyExceptionMapper implements ExceptionMapper<Exception> {

    private MyService myService;

    public MyExceptionMapper(MyService myService) {
        this.myService= myService;
    }

    @Override
    public Response toResponse(Exception e) {
        // do something using myService
    }
}

Note that the MyExceptionMapper has no default constructor as I only
want to construct it using a MyService.

In my Jersey Application I have:

public class Application extends ResourceConfig {
    
    public Application() {
        register(MyExceptionMapper.class);

but this will give an exception as Jersey want a default constructor.
I tried adding the default constructor but then the myService is not
wired.

When I add @Autowired annotation to auto wire it does work.
Also note that @Resource annotation do not work...

Is there a different way I can inject a dependency without using the
@Autowired annotation?

I'm also seeing these messages in my log:
None or multiple beans found in Spring context for type class
org.mycomp.HelloController, skipping the type.


Cheers,
Marcel