在真正丢失资源的情况下,我的API返回以下内容
{
"code": 404,
"message": "HTTP 404 Not Found"
}当我使用代码Response.status(Response.Status.NOT_FOUND).build()通过我的资源返回404时,我会得到以下作为响应的HTML
<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>我正试图弄清楚如何阻止这个意想不到的HTML,并在没有数据的情况下进行响应。
发布于 2020-08-14 15:06:20
将泽西属性ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR设置为true。
environment.jersey()
.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);公共静态最终字符串RESPONSE_SET_STATUS_OVER_SEND_ERROR
每当响应状态为4xx或5xx时,就可以在容器特定的sendError实现上的setStatus或Response之间进行选择。例如,在servlet容器上,泽西可以调用HttpServletResponse.setStatus(...)或HttpServletResponse.sendError(...)。
调用sendError(...)方法通常重置实体、响应头,并为指定的状态代码提供错误页面(例如servlet error-page配置)。但是,如果您想要后处理响应(例如通过servlet过滤器),唯一的方法是对容器响应对象调用setStatus(...)。
如果属性值为true,则在默认Response.sendError(...)之上使用方法Response.setStatus(...)。
属性值的类型为boolean。默认值是false。
配置属性的名称为"jersey.config.server.response.setStatusOverSendError".。
发布于 2020-07-28 15:24:58
我们有同样的问题,并通过将.entity(...)设置为空字符串来解决这个问题:
Response.status(NOT_FOUND).entity("").type(MediaType.APPLICATION_JSON).build()由于这是一种黑客行为,我也渴望了解更清洁的解决方案.;
https://stackoverflow.com/questions/63056592
复制相似问题