ExceptionMapper not work in grizzly

2019-05-21 21:55发布

问题:

Sorry for my poor English. I'm using grizzly and jersey to build a web application.

And I implement like this

        ErrorModel errorModel = new ErrorModel("1", "1", "1");
        WebApplicationException applicationException = (WebApplicationException) exception;
        return Response.status(applicationException.getResponse().getStatus()).type(MediaType.APPLICATION_JSON_TYPE).entity(errorModel).build();

When I visited a page which does not exist. I found that it throw a WebApplicationException. So I debug and found this method is being called and return the response above. But finally the http response is a html page which is build by grizzly. What should i do

回答1:

Make sure you have the RESPONSE_SET_STATUS_OVER_SEND_ERROR property set.

I had the same problem with grizzly and it was capturing my catching my 400 and sending back the default generic servlet error page. This was the solution for jersey 2.

public class RestApplication extends ResourceConfig {

    private static final Logger logger = Logger.getLogger(RestApplication.class.getName());

    public RestApplication() {
        // Set this property so that the 400 will still send the entity correctly.
        property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");
        registerModules();
    }