Reputation: 342
In case of a genuine missing resource, my API returns the following
{
"code": 404,
"message": "HTTP 404 Not Found"
}
When I return a 404 through my resource using the code Response.status(Response.Status.NOT_FOUND).build()
I get the following HTML as response
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Error 404 Not Found</title>
</head>
<body>
<h2>HTTP ERROR 404 Not Found</h2>
<table>
<tr>
<th>URI:</th>
<td>/v1/2/1/100</td>
</tr>
<tr>
<th>STATUS:</th>
<td>404</td>
</tr>
<tr>
<th>MESSAGE:</th>
<td>Not Found</td>
</tr>
<tr>
<th>SERVLET:</th>
<td>io.dropwizard.jersey.setup.JerseyServletContainer-21c99abf</td>
</tr>
</table>
</body>
</html>
I am trying to figure out how I can block this unintended HTML and respond with no data.
Upvotes: 2
Views: 971
Reputation: 209004
Set the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR
to true
.
environment.jersey()
.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
public static final String RESPONSE_SET_STATUS_OVER_SEND_ERROR
Whenever response status is 4xx or 5xx it is possible to choose between
sendError
orsetStatus
on container specificResponse
implementation. E.g. on servlet container Jersey can callHttpServletResponse.setStatus(...)
orHttpServletResponse.sendError(...)
.Calling
sendError(...)
method usually resets entity, response headers and provide error page for specified status code (e.g. servleterror-page
configuration). However if you want to post-process response (e.g. by servlet filter) the only way to do it is callingsetStatus(...)
on container Response object.If property value is
true
the methodResponse.setStatus(...)
is used over defaultResponse.sendError(...)
.Type of the property value is
boolean
. The default value isfalse
.The name of the configuration property is
"jersey.config.server.response.setStatusOverSendError"
.
Upvotes: 2
Reputation: 4061
We had the same issue and solved it by setting the .entity(...)
to an empty String:
Response.status(NOT_FOUND).entity("").type(MediaType.APPLICATION_JSON).build()
Since that's kind of a hack, I am also eager to learn about cleaner solution(s) ... ;)
Upvotes: 3