`Dropwizard` 的 `Response.status(Response.Status.NOT_FOUND).build()` 返回 HTML。

huangapple go评论65阅读模式
英文:

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

{
    &quot;code&quot;: 404,
    &quot;message&quot;: &quot;HTTP 404 Not Found&quot;
}

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

&lt;html&gt;

&lt;head&gt;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=utf-8&quot; /&gt;
	&lt;title&gt;Error 404 Not Found&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
	&lt;h2&gt;HTTP ERROR 404 Not Found&lt;/h2&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;th&gt;URI:&lt;/th&gt;
			&lt;td&gt;/v1/2/1/100&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;th&gt;STATUS:&lt;/th&gt;
			&lt;td&gt;404&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;th&gt;MESSAGE:&lt;/th&gt;
			&lt;td&gt;Not Found&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;th&gt;SERVLET:&lt;/th&gt;
			&lt;td&gt;io.dropwizard.jersey.setup.JerseyServletContainer-21c99abf&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;

&lt;/body&gt;

&lt;/html&gt;

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()

由于这有点像是一种技巧,我也渴望了解更清晰的解决方案… `Dropwizard` 的 `Response.status(Response.Status.NOT_FOUND).build()` 返回 HTML。

英文:

We had the same issue and solved it by setting the .entity(...) to an empty String:

Response.status(NOT_FOUND).entity(&quot;&quot;).type(MediaType.APPLICATION_JSON).build()

Since that's kind of a hack, I am also eager to learn about cleaner solution(s) ... `Dropwizard` 的 `Response.status(Response.Status.NOT_FOUND).build()` 返回 HTML。

答案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实现上选择sendErrorsetStatus。例如,在Servlet容器上,Jersey可以调用HttpServletResponse.setStatus(...)HttpServletResponse.sendError(...)
>
>调用sendError(...)方法通常会重置实体、响应标头并为指定的状态代码提供错误页面(例如,Servlet的error-page配置)。然而,如果您希望在响应后进行后处理(例如,通过Servlet过滤器),唯一的方法是在容器Response对象上调用setStatus(...)
>
>如果属性值为true,则使用Response.setStatus(...)方法覆盖默认的Response.sendError(...)方法。
>
>属性值的类型是boolean。默认值为false
>
>配置属性的名称是&quot;jersey.config.server.response.setStatusOverSendError&quot;

英文:

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 &quot;jersey.config.server.response.setStatusOverSendError&quot;.

huangapple
  • 本文由 发表于 2020年7月23日 22:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定