Jersey 客户端请求中的多个返回类型
我按以下方式使用 Jersey Client API:-
User user = webRsrc.accept(MediaType.APPLICATION_XML).post(User.class, usr);
所以我期待 User 类的对象中的响应,该类是 JAXB 带注释的类。 然而,有时我也可能会收到错误 xml,为此我创建了一个 JAXB 类 ErrorResponse。
现在的问题是,如果我的请求返回 ErrorResponse 而不是 User 的对象,我该如何处理?
我尝试这样 -
ClientResponse response=null;
try {
response = webRsrc.accept(MediaType.APPLICATION_XML).post(ClientResponse.class,usr);
User usr = response.getEntity(User.class);
}catch(Exception exp)
{
ErrorResponse err = response.getEntity(ErrorResponse.class);
}
但是当我尝试在 catch 块中使用 getEntity() 时,它会抛出以下异常
[org.xml.sax.SAXParseException: Premature end of file.]
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:107)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:532)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:491) .....
似乎在调用 getEntity() 一次后,输入流已耗尽。
I'm using Jersey Client API in the following way :-
User user = webRsrc.accept(MediaType.APPLICATION_XML).post(User.class, usr);
So I'm expecting the response in object of User class which is a JAXB annotated class.
However, at times I might also get an error xml and for that I've created a JAXB class ErrorResponse.
Now the problem is that if my request returns an object of ErrorResponse instead of User how can I handle that ?
I tried like this -
ClientResponse response=null;
try {
response = webRsrc.accept(MediaType.APPLICATION_XML).post(ClientResponse.class,usr);
User usr = response.getEntity(User.class);
}catch(Exception exp)
{
ErrorResponse err = response.getEntity(ErrorResponse.class);
}
But when I try to use getEntity() in catch block, it throws following exception
[org.xml.sax.SAXParseException: Premature end of file.]
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:107)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:532)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:491) .....
Seems like after calling getEntity() once, the inputstream is exhausted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种代码可用于管理响应中的错误消息或业务消息:
This kind of code could be used to manage Error message or business message in the response :
如果您无法更改服务器代码,可以使用
ReaderInterceptor
。这确实有一些限制,但您至少能够获取错误响应对象的内容。我正在使用 JSON,但适用相同的原则:
然后将其用作调用的一部分:
限制是:
If you can't change the server code, you can use a
ReaderInterceptor
. This does have some limitations but you will at least be able to get the contents of the error response object.I'm using JSON but the same principles apply:
Then use this as part of the invocation:
Limitations are:
我认为您错过了整个“REST 思维方式”中的一点。
简短的回答:是的,您只能调用 getEntity 一次。您需要检查返回的 HTTP 状态以了解您应该获取什么实体。
在服务器端:
,现在您的服务器返回带有 User 对象的“HTTP status OK - 200”,或者带有错误对象的错误状态。
在客户端:
您需要检查返回状态并根据 API 规范调整您的行为。
这是一个快速而肮脏的代码示例:
注意:根据返回的状态代码,此错误可能非常不同(因此需要非常不同的行为):
I think you missed a point in the whole "REST way of thinking".
Short answer: yes, you can only call getEntity once. You need to check the returned HTTP status to know what entity you should get.
On the server side:
So, now your server returns either "HTTP status OK - 200" with a User object, or an error status with an error object.
On the client side:
You need to check the return status and adapt your behavior according to the API spec.
here's a quick and dirty code sample:
NB: depending on the status code returned, this error could be very different (thus needing very different behavior):