jersey
  1. jersey
  2. JERSEY-2063

ExceptionMapper Provider does not work when managed by Spring

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: backlog
    • Component/s: extensions
    • Labels:
      None

      Description

      I've created a simple Jersey2 Spring3 project at https://github.com/marceloverdijk/jersey2-spring3.

      It contains an GlobalExceptionMapper being configured in
      applicationContenxt.xml and registered in MyApplication.java (the
      Jersey app). This GlobalExceptionMapper also has @Component and
      @Provider annotations.

      The issue is this exception mapper does not kick in. The single
      HelloResource the project contains has a resource action which throws
      an exception, but for some reason it's not handled by my
      GlobalExceptionMapper.

      Note that the HelloResource class IS registered.

        Activity

        Hide
        sanderv added a comment - - edited

        Same issue. Worked around it by extending our ResourceConfig as follows:

        MyResourceConfig.java
        Reflections reflections = new Reflections("my.package.name");
        Set<Class<?>> providers = reflections.getTypesAnnotatedWith(Provider.class);
        log.info("Registering " + providers.size() + " providers");
        registerClasses(providers);
        

        That's not to say that this shouldn't somehow get fixed

        Show
        sanderv added a comment - - edited Same issue. Worked around it by extending our ResourceConfig as follows: MyResourceConfig.java Reflections reflections = new Reflections( "my. package .name" ); Set< Class <?>> providers = reflections.getTypesAnnotatedWith(Provider.class); log.info( "Registering " + providers.size() + " providers" ); registerClasses(providers); That's not to say that this shouldn't somehow get fixed
        Hide
        LaurentGarcia added a comment -

        I met the same problem with jersey 2.11, but removing the annotation @Component, and so keeping only @Provider fixed the problem.
        Your class have to be scanned, with the configuration in your web.xml like :

        <servlet>
        	<servlet-name>jersey-spring</servlet-name>
        	<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        	<init-param>
        		<param-name>jersey.config.server.provider.packages</param-name>
        		<param-value>com.my.package1,com.my.package2</param-value>
        	</init-param>
        </servlet>
        

        NB : I didn't override javax.ws.rs.core.Application in the web.xml

        Show
        LaurentGarcia added a comment - I met the same problem with jersey 2.11, but removing the annotation @Component, and so keeping only @Provider fixed the problem. Your class have to be scanned, with the configuration in your web.xml like : <servlet> <servlet-name> jersey-spring </servlet-name> <servlet-class> org.glassfish.jersey.servlet.ServletContainer </servlet-class> <init-param> <param-name> jersey.config.server.provider.packages </param-name> <param-value> com.my.package1,com.my.package2 </param-value> </init-param> </servlet> NB : I didn't override javax.ws.rs.core.Application in the web.xml
        Hide
        Vetle Leinonen-Roeim added a comment -

        We also found that we had to remove @Component in Jersey2, not sure if this is a bug or not.

        Show
        Vetle Leinonen-Roeim added a comment - We also found that we had to remove @Component in Jersey2, not sure if this is a bug or not.
        Hide
        CoffeeAndMe added a comment -

        In my case, ExceptionMapper via @Provider DOESN'T WORK AT ALL unless I do the ...

        Reflections reflections = new Reflections("my.package.name");
        Set<Class<?>> providers = reflections.getTypesAnnotatedWith(Provider.class);
        log.info("Registering " + providers.size() + " providers");
        registerClasses(providers);
        

        ...dance. With the Reflections dance work-around Jersey 2.13 still "eats" any instance of com.fasterxml.jackson.core.JsonProcessingException. What a mess. This really needs fixed as its presence completely obliterates the value of ExceptionMapper.

        Show
        CoffeeAndMe added a comment - In my case, ExceptionMapper via @Provider DOESN'T WORK AT ALL unless I do the ... Reflections reflections = new Reflections( "my. package .name" ); Set< Class <?>> providers = reflections.getTypesAnnotatedWith(Provider.class); log.info( "Registering " + providers.size() + " providers" ); registerClasses(providers); ...dance. With the Reflections dance work-around Jersey 2.13 still "eats" any instance of com.fasterxml.jackson.core.JsonProcessingException. What a mess. This really needs fixed as its presence completely obliterates the value of ExceptionMapper.
        Hide
        bdoyle added a comment -

        I ran into this problem also and I was pointed toward another workaround. That is to use the JSR injection annotations @Named and @Inject rather than spring ones. Spring supports these too so you don't have to switch framework.

        Show
        bdoyle added a comment - I ran into this problem also and I was pointed toward another workaround. That is to use the JSR injection annotations @Named and @Inject rather than spring ones. Spring supports these too so you don't have to switch framework.
        Hide
        Vetle Leinonen-Roeim added a comment -

        This might have been fixed as part of JERSEY-2651. The example on Github is gone, so I'm unable to test it. The fix for JERSEY-2651 should fix issues various issues with Spring Components not being picked up by Jersey.

        Show
        Vetle Leinonen-Roeim added a comment - This might have been fixed as part of JERSEY-2651 . The example on Github is gone, so I'm unable to test it. The fix for JERSEY-2651 should fix issues various issues with Spring Components not being picked up by Jersey.
        Hide
        sanderv added a comment -

        @bdoyle
        The @Named and @Inject is a nice workaround, but we have standardized on Spring for a reason and are slightly underwhelmed by the @Named and @Inject support, so I think we'd prefer a solution that supports @Component and @Autowired etc.

        Show
        sanderv added a comment - @bdoyle The @Named and @Inject is a nice workaround, but we have standardized on Spring for a reason and are slightly underwhelmed by the @Named and @Inject support, so I think we'd prefer a solution that supports @Component and @Autowired etc.
        Hide
        Jakub Podlesak added a comment -

        The link https://github.com/marceloverdijk/jersey2-spring3 gives 404 unfortunately.
        Could you please provide a reproducible test case here?

        As Vetle pointed out already, this was likely already fixed with JERSEY-2651.

        Show
        Jakub Podlesak added a comment - The link https://github.com/marceloverdijk/jersey2-spring3 gives 404 unfortunately. Could you please provide a reproducible test case here? As Vetle pointed out already, this was likely already fixed with JERSEY-2651 .

          People

          • Assignee:
            Jakub Podlesak
            Reporter:
            marceloverdijk
          • Votes:
            9 Vote for this issue
            Watchers:
            15 Start watching this issue

            Dates

            • Created:
              Updated:

              Time Tracking

              Estimated:
              Original Estimate - 1 hour
              1h
              Remaining:
              Remaining Estimate - 1 hour
              1h
              Logged:
              Time Spent - Not Specified
              Not Specified