Registering a provider programmatically in jersey which implements exceptionmapper

前端 未结 3 1849
北恋
北恋 2021-01-05 13:56

How do I register my provider programmatically in jersey which implements the Exceptionmapper provided by jersey API? I don\'t want to use @Provider annotation and want to r

相关标签:
3条回答
  • 2021-01-05 14:34

    If you're using Spring and want to register the providers programmatically based on the presence of @Path and @Provider annotation you can use the following technique

    @Component
    public class JerseyConfig extends ResourceConfig {
    
      @Autowired
      private ApplicationContext applicationContext;
    
      @PostConstruct
      public init() {
    
        applicationContext.getBeansWithAnnotation(Path.class).values().forEach(
          component -> register(component.getClass())
        );
        applicationContext.getBeansWithAnnotation(Provider.class).values().forEach(
          this::register
        );
      }
    }
    
    0 讨论(0)
  • 2021-01-05 14:50

    While @paul-samsotha's answer is correct, still there is implementation trick. I want to share it and hope it will help someone.

    a) Implement your mapper:

    public class MyExceptionMapper implements ExceptionMapper<Throwable>, ResponseErrorMapper {
        ...
    

    b) make sure you declare generic type, otherwise your mapper will never be called

    public class MyExceptionMapper implements ExceptionMapper/* no generic declaration */, ResponseErrorMapper {
        ...
    

    and may trigger

    javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class: class com...MyExceptionMapper.
    

    c) Register it as resource:

    ResourceConfig config = new ResourceConfig();
    config.register(new MyExceptionMapper());
    

    or

    config.register(MyExceptionMapper.class);
    

    d) make sure you enforce processing errors as well:

    config.setProperties(new LinkedHashMap<String, Object>() {{
        put(org.glassfish.jersey.server.ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true);
    }});
    
    0 讨论(0)
  • 2021-01-05 14:52

    I'm guessing you don't have a ResourceConfig, since you seem to not be sure how to use it. For one, it is not required. If you do use it, it should be it's own separate class. There you can register the mapper.

    public class AppConfig extends ResourceConfig {
        public AppConfig() {
            register(new MyProvider());
        }
    }
    

    But you are probably using a web.xml. In which case, you can register the provider, with the following <init-param>

    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                org.foo.providers.MyProvider
            </param-value>
        </init-param>
    </servlet>
    

    Have a look at Application Deployment and Runtime Environments for more information on different deployment models. There are a few different ways to deploy applications. You can even mix and match (web.xml and ResourceConfig).

    0 讨论(0)
提交回复
热议问题