如何禁用 Websphere 的 8.5 默认 4xx 消息?

How can I disable Websphere's 8.5 default 4xx message?

我在 Websphere 8.5 中有一个 Jersey REST 应用程序。每当我的资源以 4xx 响应响应时,Webpshere(或 IBM Http Server)都会使用其默认错误消息覆盖响应 body,例如:

Error 404: Not Found

我不仅不希望响应 body 被覆盖,我希望响应 body 由我的资源生成,而且 Websphere 不更新 Content-Length:响应 header,从而在 content-length 和实际响应 body 长度之间造成不一致。

当我的资源产生 4xx 响应时,有没有办法强制 Websphere(或 IBM HTTP 服务器)覆盖响应body?

例如调用以下资源:

@Path("timeout")
public class TimeoutService {
    @GET
    @Path("withbody")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getWithBody() {
        Response.ResponseBuilder builder = Response.status(Response.Status.NOT_FOUND);

        builder.entity("{ \"status\" : \"notok\" }");
        return builder.build();
    }
}

将导致:

Error 404: No

Request Method:GET
Status Code:404 Not Found
Response Headers
$WSEP:
Connection:Keep-Alive
Content-Language:en-US
Content-Length:13
Content-Type:text/html;charset=ISO-8859-1
Date:Tue,21 Apr 2015 11:50:38 GMT>
Keep-Alive:timeout=15, max=100
X-Powered-By:Servlet/3.0

请注意默认消息是如何由于内容长度不一致而被截断的。

但我想要的是此调用以 404{ "status" : "notok" }Content-Type 设置为 application/json

来响应

是的,你可以。这是概述 Jersey 属性 的页面,需要对其进行更改以禁用 WAS 劫持您的错误:

https://java.net/jira/browse/JERSEY-2521

简而言之,你在配置Jersey时,必须将属性 org.glassfish.jersey.server.ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR设置为true