【问题标题】:403 error page is not working in my web service which is running on a Tomcat 7403 错误页面在我在 Tomcat 7 上运行的 Web 服务中不起作用
【发布时间】:2015-02-12 19:50:34
【问题描述】:

我正在使用 Jersey 开发一个 Rest Web 服务。该服务在 Tomcat 7 上运行。在我的“WebContent”文件夹下,我放置了三个 xml 文件。一个命名为error403.xml,另一个命名为error404.xml,另一个命名为error505.xml。同样在我的 web.xml 中,我有以下几行:

    <error-page>
    <error-code>403</error-code>
    <location>/error403.xml</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/error404.xml</location>
</error-page>
<error-page>
    <error-code>405</error-code>
    <location>/error405.xml</location>
</error-page>

当 Tomcat 响应由于 http 错误导致的错误请求时,404 和 405 会正确映射并获取我的文件而不是典型的 html 错误文件。因此 error404.xml 和 error405.xml 显示正确,但我的问题是,当客户端尝试连接并导致 403 错误时,Tomcat 不会将答案映射到我的 error403.xml,因此 Tomcat 发送空白(空答案)。

有人对此有任何想法吗?任何答案将不胜感激

【问题讨论】:

  • 我将更好地解释我的问题。我现在正在运行休息服务。我的问题(也是我的头疼)是在我的应用程序 web.xml 中包含错误页面之前,一切都运行正常。 Tomcat 将 http 错误映射到其典型的 html 错误答案。但如果我包含错误页面,Tomcat 有时会回答 htttp 错误 403 而不是 404 错误。我也尝试在错误页面标记中添加 error403.xml,但 Tomcat 回答以下消息:HTTP/1.1 403 Forbidden Server: Apache-Coyote/1.1 Transfer-Encoding: chunked Date: Sat,14Feb201513:54:33
  • 答案头是:HTTP/1.1 403 Forbidden Server: Apache-Coyote/1.1 Transfer-Encoding: chunked Date: Sat, 14 Feb 2015 13:54:33 GMT 正文为空:

标签: java rest tomcat jersey custom-error-pages


【解决方案1】:

我已经解决了我的问题,包括在我的 web.xml 中:

<error-page>
    <error-code>403</error-code>
    <location>/AppExceptionHandler</location>
</error-page>  

<error-page>
    <error-code>404</error-code>
    <location>/AppExceptionHandler</location>
</error-page>

<error-page>
    <error-code>405</error-code>
    <location>/AppExceptionHandler</location>
</error-page>

AppExceptionHandler 是在发生 http 错误(403、404、405)时调用的类。我想要的是当任何注释的 http 错误发生时 Tomcat 用 xml 而不是典型的 html 回复。该类的定义是:

package exception;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import connectionDB.Constants;

@WebServlet("/AppExceptionHandler")
public class AppExceptionHandler extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doPut(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doDelete(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doHead(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doOptions(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // Analyze the servlet exception
        //Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
        if (servletName == null) {
            servletName = "Unknown";
        }
        String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
        if (requestUri == null) {
            requestUri = "Unknown";
        }

        // Set response content type
        response.setContentType("application/xml");

        //Set response content type
        response.setContentType("application/xml");

        PrintWriter out = response.getWriter();
        //<?xml version="1.0" encoding="UTF-8" standalone="yes"?><MW_ERROR_RESPONSE><MW_VERSION>1.0</MW_VERSION><MW_ERRORDESC>Wrong user</MW_ERRORDESC></MW_ERROR_RESPONSE>
        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><MW_ERROR_RESPONSE><MW_VERSION>" + Constants.MW_VERSION +"</MW_VERSION><MW_ERRORDESC>");
        if(statusCode == 405){
            response.setStatus(405);
            out.write("Client Error Request - HTTP method not allowed. Please change your HTTP method in your request</MW_ERRORDESC></MW_ERROR_RESPONSE>");
        }else if(statusCode == 404){
            response.setStatus(404);
            out.write("Client Error Request - Request not found or invalid URI: " + requestUri  + ". Please change your URI request</MW_ERRORDESC></MW_ERROR_RESPONSE>");
        }else{
            response.setStatus(403);
            out.write("Client Error Request - Unknown error</MW_ERRORDESC></MW_ERROR_RESPONSE>");
        }
    }
}

所以当客户端发送一个错误的请求时,答案是:

<MW_ERROR_RESPONSE>
<MW_VERSION>1.0</MW_VERSION>
   <MW_ERRORDESC>Client Error Request - Request not found or invalid URI: /***/***. Please change your URI request</MW_ERRORDESC>
</MW_ERROR_RESPONSE>

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 2011-01-23
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多