Jersey WebApplicationException 和 servlet 过滤器
在我的应用程序中,我针对异常情况抛出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该检查“doFilter”之后的响应状态:
如果状态为 500,则回滚,就这么简单。您永远不会在 Servlet 过滤器上遇到 WebApplicationException,它们会被 Jersey Servlet 捕获。
You should inspect the response status after "doFilter":
If Status is 500 then rollback, as simple as that. You will never get WebApplicationException on Servlet filters, they are catched by Jersey Servlet.
既然你没有问具体的问题,我就简单说一下。
Since you didn't ask a specific question, I'll make some observations.