Jax-RS(泽西岛)上下文中 WebApplicationException 和 WebServiceException 的区别

发布于 2024-12-13 03:37:17 字数 173 浏览 0 评论 0原文

我正在创建一个 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 技术交流群。

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

发布评论

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

评论(1

假面具 2024-12-20 03:37:17

WebApplicationException 是一种可以停止执行 REST 资源并向客户端发送一些有意义的信息的方法。对于我一直在做的事情,我对此异常进行了子类化,以便它有一个实现,可以将 JSON 作为错误消息生成给客户端。如果出现错误情况,假设文件丢失,我可能会执行以下操作:

}catch(FileNotFoundException ex){
    throw new MyException(ex.getMessage());

在客户端上,这会生成类似以下内容的内容:

{ errorCode: 56, errorMessage: 'could not find file "input.txt"' };

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

一个示例:

public class MyException extends WebApplicationException {

public MyException(JSONObject jsonObject) {
    super(Response.status(Response.Status.OK)
            .entity(jsonObject)
            .type(MediaType.APPLICATION_JSON)
            .build());
}

然后,从代码中的任何位置您想要停止执行并将错误信息发送到客户端,执行以下操作:

}catch(FileNotFoundException ex){
    throw new MyException(new JSONObject(){{ this.put("errorCode", 4); .... }});

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:

}catch(FileNotFoundException ex){
    throw new MyException(ex.getMessage());

On the client this then would produce something like:

{ errorCode: 56, errorMessage: 'could not find file "input.txt"' };

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:

public class MyException extends WebApplicationException {

public MyException(JSONObject jsonObject) {
    super(Response.status(Response.Status.OK)
            .entity(jsonObject)
            .type(MediaType.APPLICATION_JSON)
            .build());
}

Then from anywhere in your code you want to halt execution and send the error information to the client do this:

}catch(FileNotFoundException ex){
    throw new MyException(new JSONObject(){{ this.put("errorCode", 4); .... }});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文