Jersey WebApplicationException 和 servlet 过滤器

发布于 2024-11-24 02:33:49 字数 860 浏览 0 评论 0原文

在我的应用程序中,我针对异常情况抛出 WebApplicationException,以便我可以使用 500 传回不错的消息。我遇到的问题是,我有一个用于休眠事务的 servlet 过滤器,并且注意到 WebApplicationException 在返回到 servlet 过滤器之前被捕获。我提供了 servlet 过滤器的简短版本作为示例。注意:我只有另一个 servlet 过滤器,它可以进行身份​​验证并且没有任何捕获,

    try {
        em = MauiPersistenceUtil.getEntityManager();
        em.getTransaction().begin();

        // raises WebApplicationException
        filterChain.doFilter(request, response);

        // since WebApplicationException is trapped somewhere else up 
        // the filterChain bad data is committed here
        em.getTransaction().commit();
    } catch (Throwable ex) {
        // I want the WebApplicationException to reach here
        em.getTransaction().rollback();
    }

谢谢, 赎金

编辑:瑞安·斯图尔特指出我实际上并没有问问题。我的问题是:如何在 Servlet 过滤器中判断出 Web 应用程序异常已在过滤器链中抛出?我原以为 Jersey 会重新抛出异常,但这是不正确的。

In my application, I throw WebApplicationExceptions for exception cases so that I can pass back nice messages with my 500s. The problem I am running into is that I have a servlet filter for hibernate transactions and have noticed that the WebApplicationException is being trapped before it comes back to the servlet filter. I have included a brief version of my servlet filter as an example. Note: I only have one other servlet filter which does authentication and has no catches

    try {
        em = MauiPersistenceUtil.getEntityManager();
        em.getTransaction().begin();

        // raises WebApplicationException
        filterChain.doFilter(request, response);

        // since WebApplicationException is trapped somewhere else up 
        // the filterChain bad data is committed here
        em.getTransaction().commit();
    } catch (Throwable ex) {
        // I want the WebApplicationException to reach here
        em.getTransaction().rollback();
    }

Thanks,
Ransom

Edit: Ryan Stewart pointed out that I did not actually ask a question. My question is: How can I tell in a servlet filter that a web application exception was thrown down in the filterChain? I was expecting that Jersey would rethrow the exception, but that was incorrect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-12-01 02:33:53

您应该检查“doFilter”之后的响应状态:

 filterChain.doFilter(servletRequest, servletResponse);

 Integer status = ((HttpServletResponse) servletResponse).getStatus();
 if (HttpServletResponse.SC_INTERNAL_SERVER_ERROR.equals(status) {
     em.getTransaction().rollback();
 } else {
     em.getTransaction().commit();
 }

如果状态为 500,则回滚,就这么简单。您永远不会在 Servlet 过滤器上遇到 WebApplicationException,它们会被 Jersey Servlet 捕获。

You should inspect the response status after "doFilter":

 filterChain.doFilter(servletRequest, servletResponse);

 Integer status = ((HttpServletResponse) servletResponse).getStatus();
 if (HttpServletResponse.SC_INTERNAL_SERVER_ERROR.equals(status) {
     em.getTransaction().rollback();
 } else {
     em.getTransaction().commit();
 }

If Status is 500 then rollback, as simple as that. You will never get WebApplicationException on Servlet filters, they are catched by Jersey Servlet.

别想她 2024-12-01 02:33:52

既然你没有问具体的问题,我就简单说一下。

  1. 您应该使用声明性事务,例如 Spring 提供的事务,而不是尝试以这种方式自己管理它们,因为您会遇到这样的问题。
  2. 与 1. 类似,我非常怀疑您是否希望在所有异常情况下回滚事务。再次,研究一个框架来帮助你解决这个问题。
  3. WebApplicationException 被 Jersey 拦截并处理。假设您正在以标准方式运行 Jersey,作为 servlet。过滤器围绕 servlet 执行。因此,Jersey Servlet 在将异常送出过滤器之前对其进行处理。

Since you didn't ask a specific question, I'll make some observations.

  1. You should use declarative transactions, such as those provided by Spring, instead of trying to manage them yourself in this way, exactly because you'll run into problems like this.
  2. Similar to 1., I very much doubt you will want to roll back transactions on all exceptions. Again, look into a framework to help you with this.
  3. A WebApplicationException is intercepted and handled by Jersey. Presumably you're running Jersey in the standard way, as a servlet. Filters execute around servlets. Therefore the Jersey servlet handles the exception before it makes it out the filter.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文