Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何捕获java/jax-rs中的应用程序错误_Java_Angularjs_Jax Rs_Jersey 2.0 - Fatal编程技术网

如何捕获java/jax-rs中的应用程序错误

如何捕获java/jax-rs中的应用程序错误,java,angularjs,jax-rs,jersey-2.0,Java,Angularjs,Jax Rs,Jersey 2.0,我想知道asp.net中是否存在类似Global.asax的应用程序错误,而java中是否存在 我正在使用jersey&angularJS。我已经实现了我测试过的ServletContextListener,它可以正常工作,但找不到类似application\u error的东西 public class MyListener implements ServletContextListener { @Override public void contextDestroyed(S

我想知道asp.net中是否存在类似Global.asax的应用程序错误,而java中是否存在

我正在使用jersey&angularJS。我已经实现了我测试过的ServletContextListener,它可以正常工作,但找不到类似application\u error的东西

public class MyListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("Shutting down");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("Starting");
    }

}
我还有一个类,它实现了ContainerRequestFilter中的方法过滤器,在这里我验证我的jwt令牌并接受或拒绝授权

我的所有方法都不处理内部的异常,这意味着我希望捕获整个应用程序的异常,即使它是在资源、服务层或数据访问层的方法等中生成的,并尝试向管理员发送电子邮件并重定向到error.html页面


提前感谢。

我认为有一些独立于
Jersey
的解决方案可以实现,用于捕获java web应用程序中的所有异常。我假设您的项目具有
web.xml
-

<filter>
    <filter-name>exceptionFilter</filter-name>
    <filter-class>com.MyExceptionFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>exceptionFilter</filter-name>
    <url-pattern>*</url-pattern> 
</filter-mapping> 
1) 使用过滤器-您可以在web应用程序中添加一个
过滤器
,并捕获所有异常
Filter
就像一个拦截器,可以在处理任何请求之前调用它

您可以在
web.xml
-

<filter>
    <filter-name>exceptionFilter</filter-name>
    <filter-class>com.MyExceptionFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>exceptionFilter</filter-name>
    <url-pattern>*</url-pattern> 
</filter-mapping> 
这种方法的问题是,可能在web应用程序中定义多个过滤器。调用这些过滤器的顺序不是固定的

2) 错误页面-您还可以为web应用程序定义错误页面。在
web.xml
中,添加以下行-

<error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/ExceptionHandler</location>
</error-page>
@WebServlet("/ExceptionHandler")
public class ExceptionHandler extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processException(request, response);
    }
    private void processException(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        Throwable throwable = (Throwable) request
                .getAttribute("javax.servlet.error.exception");
        // do whatever you want to do with exception        
        // forward request to error page
        request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
    }
}