I have the following stack for a REST API
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With