Java – prevent Tomcat from interfering with Jersey / jax-rs 2 response body on HTTP error status 4xx or 5xx
I have the following stack of 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 an explanation to the client for easier debugging
First, I tried to use @ context httpservletresponse and set the HTTP status code there, but Jersey ignored it (this is normal behavior, but it is beyond my understanding)
Then I try to use a 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's reward to me is as follows:
<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 standard Tomcat HTML output is limited by the context length of the response body I want to return (. Entity (b) – the length of B) So it's recognized, but Tomcat just overwrites it with its own error page
As a side note, I also try to return the response with the same result:
return Response.status(Response.Status.CONFLICT).entity(b).build()
So how can I tell Tomcat to let me pass my answer alone?
Solution
Just add the following code to the configuration of resourceconfig class
property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR,"true");
See Jersey config. server. response. setStatusOverSendError
