响应中的GetEntity与ReadEntity(Javax.ws.rs) [英] GetEntity vs ReadEntity in Response (Javax.ws.rs)
问题描述
我正在编写一个客户端来使用RestService,我必须从响应中读取一个实体,我完全混淆了两个方法中的哪个方法( getEntity
vs应该使用 readEntity
。
I am writing a client to consume a RestService and I have to read an entity out of the response and I am totally confused which method out of the two (getEntity
vs readEntity
) should be used.
每当我收到WebApplicationException时,我都必须检索实体。
I have to retrieve the entity whenever I get a WebApplicationException.
所以,我的代码或多或少看起来像。
So, my code more or less looks like.
catch(WebApplicationException ex) {
// Do something with ex.getResponse
}
来自,等等我已经测试了,
From, whatever I have tested,
ex.getResponse().hasEntity() ---> true
ex.getResponse().getEntity() == null ---> true
我不知道它是如何工作的但如果第一个是真的那么第二个陈述怎么样是的。
I don't know how it is working but if the first is true then how second statement could be true.
令人惊讶的是,readEntity对我来说很好,我能够从响应中读出
实体。
Surprisingly, readEntity worked fine for me and I was able to read the entity out from the response.
但是,在通过readEntity读取实体后,
But, after reading the entity through readEntity,
此调用给出了false。
this call gives false.
ex.getResponse().getEntity() == null --> false
有人可以帮我理解幕后真正发生的事情吗?
Can someone help me understand what is really happening behind the scenes?
推荐答案
此行为记录在 API :
public abstract< T> T readEntity(Class< T> entityType)
使用支持映射的MessageBodyReader将消息实体输入流作为指定Java类型的实例读取消息实体流到请求的类型。
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
[...]
从此方法返回的消息实例将被缓存用于后续通过getEntity()检索。
第一次调用 ex.getResponse()。getEntity ()
为null,因为尚未读取实体。在调用 readEntity()
之后,解析后的实体将被 getEntity()
解析。
The first call to ex.getResponse().getEntity()
is null, because the Entity hasn't been read yet. After calling readEntity()
, the parsed entity will be returned as parsed by getEntity()
.
这篇关于响应中的GetEntity与ReadEntity(Javax.ws.rs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!