如何在不向用户显示堆栈跟踪的情况下处理servletfilter中的错误状态?

我正在开发一个Jetty / RESTEasy应用程序。 如果我从我的一个REST端点抛出WebApplicationException(myResponse) ,它会将给定的响应发送到客户端。

当filter检测到错误时,我想要相同的行为:

  1. 它应该停止执行,并且
  2. 它应该给用户一个明确的,JSON格式的错误,不包括堆栈跟踪。

显然,只需写入响应流并从doFilter方法中return工作。 但这对doFilter调用的其他方法不起作用。

抛出任何exception将满足条件#1,但我还没有想出一个理智的方式来满足条件#2。 (你可以在底部看到我最好的尝试。)

正如Perception在他的回答中解释的那样, WebApplicationException被视为Filter的上下文中的任何其他exception,因此为用户提供了一个很好的丑陋堆栈跟踪。

那么,总结一下我的问题:

  • serveltt容器是否具有throw new WebApplicationException(Response)等价物?
  • 也许更重要的是,其他java项目如何处理这个?

我在一个filter中有这个代码,它可以工作,但我更喜欢一个更优雅的解决方案,它自动应用于所有filter:

 public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { try { doFilterOrThrow(request, response, chain); } catch (WebApplicationException e) { Response res = e.getResponse(); ((HttpServletResponse) response).sendError(res.getStatus(), (String) res.getEntity()); } } 

您提到的Web应用程序exception的具体处理仅在JAX-RS容器的上下文中定义,顺便说一下,它与Servlet容器不同。

Webfilter由Servlet容器处理,该容器不知道或不关心同一应用程序服务器中是否存在JAX-RS容器。 它也不知道或不关心Web应用程序exception。 因此,当您从filter中抛出WAE时,它将被视为与任何其他exception相同(带有堆栈跟踪的服务器错误,或者如果您在Web应用程序中设置了一个预配置的错误页面)。

在我看来,如果您通过filter向客户端指示错误,您可以直接写入响应流。 但是,如果您正在尝试利用某些现有的JAX-RS逻辑,那么(RESTEasy特定的)解决方案是将请求标记为filter中的错误,然后使用提供程序类在JAX-RS中生成WAE。 例:

 @WebFilter(urlPatterns = "*") public class ForwardingFilter implements Filter { @Override public void destroy() { return; } @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { // Add an error response to be processed by the JAX-RS container. // This would obviously be based on some condition. request.setAttribute("errorResponse", Response.status(500).entity("Didn't work out!").build()); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { return; } } @Provider @ServerInterceptor @HeaderDecoratorPrecedence @RequestScoped public class ForwardingHandlerProvider implements PreProcessInterceptor { @Override public ServerResponse preProcess(final HttpRequest request, final ResourceMethod method) throws Failure, WebApplicationException { final Response errorResponse = (Response) request .getAttribute("errorResponse"); if (errorResponse != null) throw new WebApplicationException(errorResponse); return null; } } 

由于提供程序存在于JAX-RS域中,因此Web应用程序exception将根据JAX-RS规范的第3.3.4节的规则进行处理,并在客户端获得所需的响应。

*编辑:*

最重要的是,没有标准的Java EE规定方式(当前)以集中方式处理servletexception,类似于JAX-RS中可用的方式。 既然你正在使用JBoss / RestEASY,你可以利用JBoss Seam Catch库来获得非常接近的结果。

 @HandlesExceptions public class ExceptionHandler { public void handleServletException( final @Handles @WebRequest CaughtException caught, @Context final HttpServletResponse response) { try { response.sendError(500, "An error occured"); } catch (final IOException ioe) { System.err.println("Dumb IO Exception: " + ioe); } } } 

上面说明了一个exception处理程序,如Seam Catch文档中所述 。 请注意,图书馆目前处于巨大变化之中,因此您只想将其作为最后的手段使用。