Registering a provider programmatically in jersey which implements exceptionmapper

前端 未结 3 1850
北恋
北恋 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
        );
      }
    }
    

提交回复
热议问题