英文:
Dropwizard Response.status(Response.Status.NOT_FOUND).build() returns html
问题
如果出现真正缺少资源的情况,我的API将返回以下内容:
{
"code": 404,
"message": "HTTP 404 Not Found"
}
当我通过以下代码在我的资源中返回404时:
Response.status(Response.Status.NOT_FOUND).build()
我会得到以下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,并以无数据的方式响应。
英文:
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.
答案1
得分: 3
我们曾经遇到过相同的问题,并通过将 .entity(...)
设置为空字符串来解决它:
Response.status(NOT_FOUND).entity("").type(MediaType.APPLICATION_JSON).build()
由于这有点像是一种技巧,我也渴望了解更清晰的解决方案…
英文:
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) ...
答案2
得分: 2
将Jersey属性ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR
设置为true
。
environment.jersey()
.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
> public static final String RESPONSE_SET_STATUS_OVER_SEND_ERROR
>
>无论响应状态是4xx还是5xx,都可以在容器特定的Response
实现上选择sendError
或setStatus
。例如,在Servlet容器上,Jersey可以调用HttpServletResponse.setStatus(...)
或HttpServletResponse.sendError(...)
。
>
>调用sendError(...)
方法通常会重置实体、响应标头并为指定的状态代码提供错误页面(例如,Servlet的error-page
配置)。然而,如果您希望在响应后进行后处理(例如,通过Servlet过滤器),唯一的方法是在容器Response对象上调用setStatus(...)
。
>
>如果属性值为true
,则使用Response.setStatus(...)
方法覆盖默认的Response.sendError(...)
方法。
>
>属性值的类型是boolean
。默认值为false
。
>
>配置属性的名称是"jersey.config.server.response.setStatusOverSendError"
。
英文:
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
or setStatus
on container specific Response
implementation. E.g. on servlet container Jersey can call HttpServletResponse.setStatus(...)
or HttpServletResponse.sendError(...)
.
>
>Calling sendError(...)
method usually resets entity, response headers and provide error page for specified status code (e.g. servlet error-page
configuration). However if you want to post-process response (e.g. by servlet filter) the only way to do it is calling setStatus(...)
on container Response object.
>
>If property value is true
the method Response.setStatus(...)
is used over default Response.sendError(...)
.
>
>Type of the property value is boolean
. The default value is false
.
>
>The name of the configuration property is "jersey.config.server.response.setStatusOverSendError"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论