Java 条件错误页';在web.xml/skip错误页中使用

Java 条件错误页';在web.xml/skip错误页中使用,java,jersey,web.xml,Java,Jersey,Web.xml,我有一个同时使用JSF和Jersey的Web应用程序: /contextpath/rest/whatever -> Jersey /contextpath/everythingelse -> JSF 如果JSF、ie 500内部服务器错误中出现错误,则会显示一个错误页面,原因是web.xml中的配置 ... <error-page> <error-code>403</error-code> <location>forbid

我有一个同时使用JSF和Jersey的Web应用程序:

/contextpath/rest/whatever -> Jersey
/contextpath/everythingelse -> JSF
如果JSF、ie 500内部服务器错误中出现错误,则会显示一个错误页面,原因是web.xml中的配置

...
<error-page>
   <error-code>403</error-code>
   <location>forbidden.jsp</location>
</error-page>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/path/to/errorhandler.jsp</location>
</error-page>
。。。
403
禁止的.jsp
java.lang.Throwable
/path/to/errorhandler.jsp
在“JSF陆地”中,这项功能可以正常工作。但是,如果Jersey资源引发异常:

  • ExceptionMapper(Jersey)处理异常,并
  • 发送错误响应(例如403禁止)
  • 由于它是在web.xml中定义的,因此将提供禁止的.jsp页面
  • 这有一个不良的副作用,即调用planized.jsp并将HTML返回给请求application/json的客户机。我的第一个想法是有条件地编写错误页语句,这样它们只会占用非rest资源,但这似乎是不可能的


    其他建议?

    所以,如果8年后有人仍然面临这个问题(是的,我也不为此感到骄傲…),我就是这样解决的:

    向web.xml添加了一个错误页,该错误页以servlet为目标:

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

    我有同样的问题。如何防止Jersey抛出的异常被发送到错误页面?
    @WebServlet("/403Handler")
    public class 403Handler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        processError(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        processError(request, response);
    }
    
    private void processError(HttpServletRequest request, HttpServletResponse response) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode != 403) {
            return;
        }
    
        String originalUrl = (String) request.getAttribute("javax.servlet.error.request_uri");
        if (StringUtils.startsWith(originalUrl, "contextpath/rest")) {
            return;
        }
    
        try {
            request.getRequestDispatcher("/path/to/errorhandler.jsp").forward(request, response);
        } catch (ServletException | IOException e) {
            log.error("failed to foward to error handler", e);
        }
    }