users@jersey.java.net

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

From: Ivar <ivarconr_at_gmail.com>
Date: Fri, 23 Aug 2013 08:10:45 +0200

Hi Macel,

the spring3-integration module has already been discussed in a previous
thread. After looking at the source code for the integrating module i found
that jersey will pick up the bean from your spring context if you have
annotated it with the "@Component" annotation. I have also demonstrated
this on github: https://github.com/ivarconr/jersey2-spring3-webapp

So just add the "@Component" to the ExceptionMapper-class and remember to
wire it up with your spring-config.

Good luck!

Ivar



Med vennlig hilsen,

Ivar Conradi Ă˜sthus
920 43 382
ivarconr_at_gmail.com


On 22 August 2013 08:58, <marceloverdijk_at_gmail.com> wrote:

> 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
>