简体   繁体   中英

how to trigger ExceptionMapper in jersey 2?

I have a problem with my 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. It doesn't enter my errorhandler.

邮递员错误页

The solution was to remove the @Component annotation. For some reason, the ExceptionMapperFactory didn't pick up my class when it had double annotations like this. I didn't think it would be a problem, but it was. It now works (and I know a little more about the ExceptionMapperFactory).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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