如果确实缺少资源,我的 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 并在没有数据的情况下进行响应。
我们遇到了同样的问题并通过设置解决了它.entity(...)
到一个空字符串:
Response.status(NOT_FOUND).entity("").type(MediaType.APPLICATION_JSON).build()
由于这是一种黑客行为,我也渴望了解更清洁的解决方案......;)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)