简体   繁体   English

如何在球衣2中触发ExceptionMapper?

[英]how to trigger ExceptionMapper in jersey 2?

I have a problem with my ExceptionMapper: 我的ExceptionMapper有问题:

@Provider
@Component
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

    public GenericExceptionMapper() {
        logger.debug("hej hopp");

    }

    @Override
    public Response toResponse(Throwable exception) {
        ErrorMessage errorMessage = new ErrorMessage("Technical error", 
            "An unknown technical error occured.");
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
            .entity(errorMessage).build();
    }
}

It doesn't catch any exceptions from my resources. 它不会从我的资源中捕获任何异常。 I have placed a debug point in the constructor and see that it is created. 我在构造函数中放置了一个调试点,并看到它已创建。 I have debuged the request and it doesn't seem to look for any exception handlers. 我已经调试了请求,它似乎没有寻找任何异常处理程序。

This is part of my configuration: 这是我的配置的一部分:

@ComponentScan("com.companywhereiwork.application")

And another part: 另一部分:

        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", 
               "com.companywhereiwork.application");

When I throw an exception from inside one of my resources: 当我从自己的资源之一中引发异常时:

    if (Math.random() < 1f) {
        throw new RuntimeException("Blä");
    }

It is returned to the client as a tomcat errorpage. 它作为tomcat错误页面返回给客户端。 It doesn't enter my errorhandler. 它没有进入我的错误处理程序。

邮递员错误页

The solution was to remove the @Component annotation. 解决方案是删除@Component批注。 For some reason, the ExceptionMapperFactory didn't pick up my class when it had double annotations like this. 由于某种原因,当ExceptionMapperFactory具有这样的双注解时,它没有接收我的类。 I didn't think it would be a problem, but it was. 我不认为这是个问题,但是确实如此。 It now works (and I know a little more about the ExceptionMapperFactory). 现在可以正常工作了(我对ExceptionMapperFactory有了更多的了解)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM