为什么用Java/Jersey返回404响应时会得到静态文本/html?

为什么用Java/Jersey返回404响应时会得到静态文本/html?,java,google-app-engine,jersey,jersey-2.0,Java,Google App Engine,Jersey,Jersey 2.0,我正在使用Java、Jetty和Jersey 2.18(目前最新版本),它们托管在Google应用程序引擎上 比如说,我有这样的服务 @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{userId}") public Response getUser(@PathParam("userId") String userId) { ... } 当我这样做时: return Response.ok() .en

我正在使用Java、Jetty和Jersey 2.18(目前最新版本),它们托管在Google应用程序引擎上

比如说,我有这样的服务

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}")
public Response getUser(@PathParam("userId") String userId)
{
    ...
}
当我这样做时:

    return Response.ok()
            .entity(user)
            .build();
我正确地接收到应用程序/json内容类型和正文。 但当我这样做的时候:

    return Response
            .status(404)
            .entity(new ResponseModel(100, "user not found"))
            .build();
与返回任何4XX或5XX状态相同,我收到一个文本/html内容类型以及此html正文:

<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>404 Not Found</title>
  </head>
  <body text=#000000 bgcolor=#ffffff>
    <h1>Error: Not Found</h1>
  </body>
</html>

是否可以使用以下配置重试:

<servlet>
    <servlet-name>API</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    ...
    <init-param>
        <param-name>jersey.config.server.response.setStatusOverSendError</param-name>
        <param-value>true</param-value>
   </init-param>
</servlet>

美国石油学会
org.glassfish.jersey.servlet.ServletContainer
...
jersey.config.server.response.setStatusOversender错误
真的
这定义了Jersey在发送4xx或5xx响应状态时是否使用
ServletResponse.sendError
(标志为
false
)或
ServletResponse.setStatus
(标志为
true

调用
ServletResponse.sendError
通常会重置响应实体和标题,并返回状态代码的(text/html)错误页面


由于您希望返回自己的自定义错误实体,因此需要将此标志设置为
true

在我看来不是重复的。另一个问题是“如何获得一个错误”,这是“在发生错误后如何控制MIME类型”,这在其他答案中没有涉及。正是@o11c谢谢通常当Java服务器返回这样一个错误时,意味着URL无法识别。你确定请求到达你的代码(getUser方法)并被执行了吗?@KamenStoykov是的,调用通过我的方法,我的代码被执行(我可以在谷歌应用程序引擎日志中看到日志和错误日志),也许你的Jetty被配置为具有自定义错误页面?检查你的${jetty.home)/contexts目录。哦,我的天哪,它似乎起作用了!!寻找关于这个配置步骤的更多文档,我发现了这个页面:请编辑你的答案,包括更多信息和来源,我会接受它(考虑到明天早上我醒来时它仍然有效:-)谢谢@wero
import java.io.Serializable;


public class ResponseModel implements Serializable
{
    private static final long   serialVersionUID    = 1L;

    private int                 code;
    private Serializable        data;

    public ResponseModel()
    {
    }

    public ResponseModel(int code, Serializable data)
    {
        System.err.println("Code " + code + " : " + data);
        this.code = code;
        this.data = data;
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public Serializable getData()
    {
        return data;
    }

    public void setData(Serializable data)
    {
        this.data = data;
    }
}
<servlet>
    <servlet-name>API</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    ...
    <init-param>
        <param-name>jersey.config.server.response.setStatusOverSendError</param-name>
        <param-value>true</param-value>
   </init-param>
</servlet>