如何处理JAX-RS中的意外异常 [英] How to handle unexpected exceptions in JAX-RS

查看:279
本文介绍了如何处理JAX-RS中的意外异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的技术堆栈是Tomcat(servlet 3.0),Jersey用于JAX-RS,Spring和Hibernate。当Hibernate中的某些数据转换错误出现意外问题时,我不希望客户端看到我的堆栈跟踪是否存在Tomcat尝试默认打印的异常。但是我想记录这些异常,这样我就可以找到错误并解决问题。

My technology stack is Tomcat (servlet 3.0), Jersey for JAX-RS, Spring and Hibernate. When something unexpect goes wrong like some data conversion error in Hibernate, I do not want the clients to see my stacktrace for those exceptions that the Tomcat tries to print by default. However I would like to log those exceptions so I can find what is wrong and fix things.

我的第一次尝试是使用来自JAX-RS的ExceptionMapper并且天真地认为会解决我的问题。但后来我发现泽西岛抛出了自己的例外,例如未映射的网址是com.sun.jersey.api.NotFoundExceptions。这会导致记录404异常,这是我不想要的。更糟糕的是,客户端不再获得404但是状态代码为500.

My first try was using ExceptionMapper from JAX-RS and naively thinking that would solve my problems. But then I noticed that Jersey throws it's own exceptions for example urls that are not mapped are com.sun.jersey.api.NotFoundExceptions. This causes 404 exceptions to be logged which I don't want. Worse, the client no longer gets 404 but a status code 500.

我可以为jersey抛出的异常创建异常映射器,但我可能会遗漏一些东西。是否有这样的最佳实践?

I could just create exception mappers for the exceptions that jersey throws but I could miss something. Is there a best practice for something like this?

推荐答案

Jersey直接抛出的所有异常都是WebApplicationException的子类,所以如果你想要要抓住它们,只需为该类创建一个ExceptionMapper,你就会很好。

All exceptions thrown directly by Jersey are subclasses of WebApplicationException, so if you want to catch them all just create an ExceptionMapper for that class and you'll be good.

这篇关于如何处理JAX-RS中的意外异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆