java - 403 错误页面在我的 Tomcat 7 上运行的 Web 服务中不起作用

标签 java rest tomcat jersey custom-error-pages

我正在使用 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 中包含以下内容:

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

关于java - 403 错误页面在我的 Tomcat 7 上运行的 Web 服务中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28486525/

相关文章:

java - JPMS : --add-opens doesn't work for java. lang.reflect.InaccessibleObjectException

JavaFX TableView 动态列和数据值

java - 基于GWT的开源电子商务解决方案

java - 尝试用 Java 实现一个简单的 JSON Web 服务

java - tomcat如何处理重启之间的 session ?

java 垃圾回收和空引用

node.js - 如果未找到 ID,Mongoose 更新文档或插入新文档(Express REST API)

java - 使用 JavaEE 7 中的 JSON 处理创建业务对象

session - 跨子域的 Tomcat SSO

java - Tomcat 7 session 超时但 postgres sql 连接保持 Activity 状态