Jax-RS(泽西岛)上下文中 WebApplicationException 和 WebServiceException 的区别
我正在创建一个 Jersey Web 服务,并且我发现自己使用了上述两种异常类型。 WebServiceException 的构造函数允许您传递一个 String 作为原因,而 WebApplicationException 允许传入 HTTP 状态代码。包括构造函数的差异,拥有这两种异常类型的目的是什么?
谢谢。
I'm creating a Jersey web service, and I've found myself using both of the mentioned exception types. WebServiceException's constructor allows you to pass a String as the cause where WebApplicationException allows a HTTP status code to be passed in. Including constructor differences, what's the purpose of having these two exception types?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WebApplicationException 是一种可以停止执行 REST 资源并向客户端发送一些有意义的信息的方法。对于我一直在做的事情,我对此异常进行了子类化,以便它有一个实现,可以将 JSON 作为错误消息生成给客户端。如果出现错误情况,假设文件丢失,我可能会执行以下操作:
在客户端上,这会生成类似以下内容的内容:
http://download.oracle.com/javaee/6/api/javax/ws/rs/WebApplicationException.html'
WebServiceException 是根运行时泽西岛是个例外,即它是最常见的由于资源崩溃并导致 HTTP 500。
http://download. oracle.com/javaee/5/api/javax/xml/ws/WebServiceException.html
所以简短的答案是第一个异常是您可能抛出的一个异常,另一个是您希望永远不会抛出的异常。 :P
一个示例:
然后,从代码中的任何位置您想要停止执行并将错误信息发送到客户端,执行以下操作:
A WebApplicationException is a way in which you may stop execution of a REST resource and send some meaningful information to your client. For the stuff I have been doing I subclassed this exception so that it has an implementation that produces JSON as error messages to the client. In the event of an error condition, let us say a missing file I might do something like this:
On the client this then would produce something like:
http://download.oracle.com/javaee/6/api/javax/ws/rs/WebApplicationException.html'
A WebServiceException is the root run time exception for Jersey, i.e. its what most commonly results from your resources crashing and results in a HTTP 500.
http://download.oracle.com/javaee/5/api/javax/xml/ws/WebServiceException.html
So the short answer is the first exception is one you might throw and the other is one you hope is never thrown. :P
An example:
Then from anywhere in your code you want to halt execution and send the error information to the client do this: