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.
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:
ClientResponse response=null;
response = webRsrc.accept(MediaType.APPLICATION_XML).post(ClientResponse.class,usr);
int status = response.getStatus();
if (Response.Status.OK.getStatusCode() == status) {
// normal case, you receive your User object
User usr = response.getEntity(User.class);
} else {
ErrorResponse err = response.getEntity(ErrorResponse.class);
}
NB: depending on the status code returned, this error could be very different (thus needing very different behavior):
This kind of code could be used to manage Error message or business message in the response :
protected <T> T call(String uri, Class<T> c) throws BusinessException {
WebResource res = new Client().create().resource(url);
ClientResponse cresp = res.get(ClientResponse.class);
InputStream respIS = cresp.getEntityInputStream();
try {
// Managing business or error response
JAXBContext jCtx = JAXBContext.newInstance(c, BeanError.class);
Object entity = jCtx.createUnmarshaller().unmarshal(respIS);
// If the response is an error, throw an exception
if(entity instanceof BeanError) {
BeanError error = (BeanError) entity;
throw new BusinessException(error);
// If this not an error, this is the business response
} else {
return (T) entity;
}
} catch (JAXBException e) {
throw(new BusinessException(e));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With