Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Tomcat from interfering with Jersey/JAX-RS 2 Response Body on HTTP Error Status 4xx or 5xx

I have the following stack for a REST API

  • Jersey 2/JAX RS 2.0
  • Tomcat 7.0.47
  • Jackson 2

My goal is to have a custom response body when an error occurs. I want to be able to send the client an explanation what exactly went wrong for easier debugging.

First I tried to use @Context HttpServletResponse and set the http status code there, but it was ignored by jersey (which is the normal behaviour but this is beyond my understanding)

Then I tryed to use WebApplicationException like this:

@GET
@Path("/myapi")
public BaseResponse getSomething() {
   BaseResponse b = new BaseResponse();
   if(error) {
      b.setStatusDescription("reason for error");
      throw new WebApplicationException(Response.status(Response.Status.CONFLICT).entity(b).build());
   }
   //add content to BaseReponse
   return b
}

But Tomcat returns me somthing like this:

<html>
    <head>
        <title>Apache Tomcat/7.0.47 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22p

Which is the standard Tomcat html output capped by the contet-length of response body I wanted to return (.entity(b) - the length of b). So it is recognized but Tomcat just overwrites it with its own error page.

As a side note I also tried to just return the Response with the same outcome:

return Response.status(Response.Status.CONFLICT).entity(b).build()

So how do I tell Tomcat to leave me alone and let my own responses through?

like image 954
Patrick Favre Avatar asked Jan 15 '14 10:01

Patrick Favre


1 Answers

Just add the following code in the configuration of ResourceConfig class.

property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");

See jersey.config.server.response.setStatusOverSendError

like image 98
Flavio Junior Avatar answered Nov 03 '22 04:11

Flavio Junior