gpt4 book ai didi

java - Dropwizard Response.status(Response.Status.NOT_FOUND).build() 返回 html

转载 作者:行者123 更新时间:2023-12-02 19:21:01 29 4
gpt4 key购买 nike

如果确实缺少资源,我的 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 并在没有数据的情况下进行响应。

最佳答案

我们遇到了同样的问题,并通过将 .entity(...) 设置为空字符串来解决它:

Response.status(NOT_FOUND).entity("").type(MediaType.APPLICATION_JSON).build()

由于这是一种黑客行为,我也渴望了解更清洁的解决方案......;)

关于java - Dropwizard Response.status(Response.Status.NOT_FOUND).build() 返回 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63056592/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com