简体   繁体   English

JAX-RS客户端:ResponseProcessingException处理

[英]JAX-RS client: ResponseProcessingException handling

Some overloaded call request methods, such as: get() and post(Entity<?> entity) (there are others) of SyncInvoker return a Response object, rather than the unmarshalled content. 一些重载的调用请求方法,例如: get()post(Entity<?> entity) (还有其他) SyncInvoker返回一个Response对象,而不是unmarshalled内容。

I noticed that in the case of get() , there is no documented ResponseProcessingException , while other methods, such as all 3 overloaded post methods, may throw a ResponseProcessingException . 我注意到在get()的情况下,没有记录的ResponseProcessingException ,而其他方法,例如所有3个重载的post方法,可能抛出ResponseProcessingException

I'm aware that ResponseProcessingException is a RuntimeException which inherits from ProcessingException , but I still would interpret this to mean that the get() method won't throw a ResponseProcessingException . 我知道ResponseProcessingException是一个RuntimeException ,它继承自ProcessingException ,但我仍然会解释这意味着get()方法不会抛出ResponseProcessingException

Is this correct? 它是否正确? What about ClientResponseFilter ? ClientResponseFilter怎么样? Why is the behavior different than the behavior of the other call request methods ( put , post ,..)? 为什么行为与其他调用请求方法( putpost ,...)的行为不同?

Also, the Javadoc for the methods which do throw a ResponseProcessingException says: 另外,抛出ResponseProcessingException的方法的Javadoc说:

in case processing of a received HTTP response fails (eg in a filter or during conversion of the response entity data to an instance of a particular Java type). 在接收到的HTTP响应的处理失败的情况下(例如,在过滤器中或在将响应实体数据转换为特定Java类型的实例期间)。

The part: 那个部分:

or during conversion of the response entity data to an instance of a particular Java type 或者在将响应实体数据转换为特定Java类型的实例期间

seems to be wrong here, as the readEntity method should not yet have been called: 在这里似乎是错误的,因为readEntity应该调用readEntity方法:

https://jersey.java.net/documentation/latest/filters-and-interceptors.html#d0e9915 https://jersey.java.net/documentation/latest/filters-and-interceptors.html#d0e9915

Is this a copy & paste documentation error? 这是复制和粘贴文档错误吗?

I guess a filter would be a valid case, though. 不过,我猜过滤器是一个有效的案例。

The documentation is certainly inconsistent. 文档肯定是不一致的。 It's clear that ResponseProcessingException is intended to be thrown when a ClientResponseFilter fails. 很明显,当ClientResponseFilter失败时,会抛出ResponseProcessingException

The implementation I'm looking at (RESTEasy 3.0.16) does this: 我正在看的实现(RESTEasy 3.0.16)这样做:

try {
    filter.filter(requestContext, responseContext);
} catch (ResponseProcessingException e) {
    throw e;
} catch (Throwable e) {
    throw new ResponseProcessingException(response, e);
}

There is no reason that the get method would not declare the exception when the put and post methods do. 没有理由在putpost方法时get方法不会声明异常。 Internally they are all handled by the same code. 在内部,它们都由相同的代码处理。

My conclusion is that the slight difference in documentation between the methods is just an oversight. 我的结论是,方法之间文档的细微差别只是一种疏忽。

Interestingly, in my copy of the source code, the get() method has this line in its javadoc: 有趣的是,在我的源代码副本中, get()方法在其javadoc中有这一行:

/**
 * @throws javax.ws.rs.ProcessingException
 *          in case the invocation processing has failed.

While all the other similar methods (eg get(Class<T>) ) are documented like this: 所有其他类似的方法(例如get(Class<T>) )都记录如下:

/**
 * @throws ProcessingException         in case the request processing or subsequent I/O operation fails.

What catches my eye is the fully qualified class name in the first one. 引起我注意的是第一个完全限定的类名。 Just a hunch, but that makes me think it was put in at a different time or by a different person. 只是预感,但这让我觉得它是在不同的时间或不同的人。 Maybe I'm overanalysing. 也许我已经过度分析了。 I tried to look at the revision history, but all I found was a single commit saying "move source code to its own repository"). 我试着查看修订历史记录,但我发现的只是一个提交说“将源代码移动到自己的存储库”。 So much for that. 这么多。

However, as you pointed out, it's not an error, since ResponseProcessingException is and a subclass of ProcessingException and isn't even a checked exception anyway. 但是,正如您所指出的那样,这不是错误,因为ResponseProcessingExceptionProcessingException的子类,并且甚至不是检查过的异常。

If you don't want your exception wrapped in ResponseProcessingException , you can make your exception extend it, then it will come without wrapping. 如果你不希望你的异常包含在ResponseProcessingException ,你可以让你的异常扩展它,然后它将没有包装。 Of course, this is feasible only if you use your own exception and you are OK with RuntimeException. 当然,只有在使用自己的异常且RuntimeException正常时,这才是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM