2

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • If this mapper is hit, and then between the time of initially calling this method (`toResponse`) and through the rest of the response cycle, if another exception is thrown, it will bubble up to the container level and the container will handle the exception. This is to protect us from infinite loops. This may be what's happening. Maybe there is some exception occurring during the serialization of the response body. Who knows. Just a guess. For better help, post an [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Paul Samsotha Mar 02 '15 at 06:22

1 Answers1

5

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();
    }
Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65