ExceptionMapper not work in grizzly

强颜欢笑 提交于 2019-12-08 00:27:37

问题


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();
    }


来源:https://stackoverflow.com/questions/28803204/exceptionmapper-not-work-in-grizzly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!