Jersey ExceptionMapper doesn't map exceptions

前端 未结 4 1324
逝去的感伤
逝去的感伤 2020-12-19 04:05

my Spring Boot + Jersey REST service doesn\'t work as expected.

EmailExistsException is thrown in UserController but I only receive error 500. All the time. And my e

相关标签:
4条回答
  • 2020-12-19 04:48

    I had the same problem

    I just mentioned the root package on the web.xml file in <param-value> tag of <init-param>

    Then It started working like charm

    <servlet>
        <servlet-name>Jersey Web Application</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.two95.restful</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    0 讨论(0)
  • 2020-12-19 04:50

    Have you configured custom ExceptionMapper as a jax-rs provider, and Are you sure that your exception is getting wrapped into EmailExistsException? You may have to look at this post.

    JAX-RS using exception mappers

    0 讨论(0)
  • 2020-12-19 05:00

    Answer that solved my problems:

    I've put packages("package.name"); which is my root package name and exception mappers work like a charm.

    @Component
    @ApplicationPath("/api")
    public class JerseyConfig extends ResourceConfig
    {
    public JerseyConfig()
    {
        packages("package.name");
        register(UserController.class);
        register(TimezoneController.class);
    }
    }
    
    0 讨论(0)
  • 2020-12-19 05:06

    If you using ExceptionMapper, you must register your exception mapper:

    @Component
    @Provider
    public class ApiApplicationExceptionMapper implements ExceptionMapper<Exception> {
    
        @Override
        public Response toResponse(Exception exception) {
           ...
        }
    }
    

    In Jersey config class, scan Jersey Endpoint class with @Path annotation and custom Exception Mapper with @Provider annotation to register:

    @Configuration
    public class JerseyConfig extends ResourceConfig {
    
        @Autowired
        ApplicationContext applicationContext;
    
        @PostConstruct
        public void init() {
            this.registerEndpoints();
            this.registerExceptionMappers();
        }
    
        private void registerEndpoints() {
            Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Path.class);
            for (Object apiClass : beans.values()) {
                logger.info("Jersey register: " + apiClass.getClass().getName());
                register(apiClass);
            }
        }
    
        private void registerExceptionMappers() {
            Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Provider.class);
            for (Object exceptionMapper : beans.values()) {
                logger.info("Jersey exception mapper register: " + exceptionMapper.getClass().getName());
                register(exceptionMapper);
            }
        }
    
    0 讨论(0)
提交回复
热议问题