【问题标题】:Why do I get static text/html when returning a 404 Response with Java / Jersey?为什么在使用 Java/Jersey 返回 404 响应时会得到静​​态文本/html?
【发布时间】:2015-06-26 13:48:18
【问题描述】:

我正在使用托管在 Google App Engine 上的 Java、Jetty 和 Jersey 2.18(目前是最新版本)。

假设我有这样的服务

@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 状态相同,我会收到一个 text/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>

而不是我放在 .entity() 中的对象

编辑:这是我的 web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app
    version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.mypackage.services;org.codehaus.jackson.jaxrs</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                org.glassfish.jersey.server.gae.GaeFeature;
                org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
                org.glassfish.jersey.media.multipart.MultiPartFeature;
            </param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Trace</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattytern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>ObjectifyFilter</filter-name>
        <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ObjectifyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring Security Filter -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml  </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>GlobalResponseFilter</filter-name>
        <filter-class>com.mypackage.GlobalResponseFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>GlobalResponseFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>everything</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

    <!-- ** -->
    <!-- ** General session timeout in minutes -->
    <!-- ** -->

    <session-config>
        <session-timeout>1440</session-timeout>
    </session-config>    
</web-app>

ResponseModel 只是一个基本的可序列化 java 类:

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;
    }
}

【问题讨论】:

  • 在我看来不像是重复的。另一个问题是“如何完全出错”,这是“出错后如何控制 MIME 类型”,其他答案未涵盖。
  • 正是@o11c 谢谢
  • 通常当Java服务器返回这样的错误时,意味着该URL未被识别。您确定该请求来自您的代码(getUser 方法)并被执行吗?
  • @KamenStoykov 是的,调用通过我的方法并且我的代码被执行(我实际上可以在谷歌应用引擎日志中看到日志和错误日志)
  • 也许您的 Jetty 已配置为具有自定义错误页面?检查您的 ${jetty.home)/contexts 目录。 wiki.eclipse.org/Jetty/Howto/Custom_Error_Pages

标签: java google-app-engine jersey jersey-2.0


【解决方案1】:

您可以使用以下配置再试一次吗:

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

这个flag 定义了 Jersey - 在发送 4xx 或 5xx 响应状态时 - 使用 ServletResponse.sendError(标志为 false)或 ServletResponse.setStatus(标志为 true)。

调用ServletResponse.sendError 通常会重置响应实体和标头并返回状态码的(文本/html)错误页面。

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

【讨论】:

  • 天哪,它似乎有效!寻找有关此配置步骤的更多文档,我找到了此页面:jersey.java.net/documentation/latest/appendix-properties.html 请编辑您的答案,包括更多信息和来源,我会接受它(考虑到明天早上我醒来时这仍然有效:-))谢谢@wero
  • 即使在 2022 年也能挽救生命,对于那些通过 Java 使用 Jersey 配置的人,您可以将其添加到 Jersey 配置中:property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
猜你喜欢
  • 2017-09-10
  • 1970-01-01
  • 2013-01-02
  • 2014-12-17
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多