我有一个网络应用,实现为:
public class ServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(
new ServletModule(){
@Override
protected void configureServlets() {
filter("*").through(new MyFilter());
server("*").with(new MyServlet());
}
};
);
}
}
然后将MyFilter
定义为:
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse {
throw new HttpErrorException(404, "not authorized");
}
@Override
public void destroy() {
}
}
当我访问http://localhost
时,抛出500错误。我想通过GuiceFilter捕获过滤器引发的异常,并将输出重写为404错误。我找不到发现这样的例外。我知道Web.xml允许我使用XML定义异常处理程序,但是如何使用GuiceFilter做到这一点?