You should return Response as the result of the interface method instead of the plain DTO.
I'm not sure about the level of control you're expecting (considering your reply to @peeskillet comment), but the Response object will give you the opportunity to fine tune your server's response (headers, cookies, status etc.) and read all of them at the client side - as you might see taking a look at Response's members like getStatus()
and getHeaders()
.
The only gotcha here is how to get the body. For this, I'd tell you to use readEntity(Class<T>)
(not the getEntity()
method as one might try at first). As long as you have the right media type provider registered, you can extract the entity as your DTO class in a easy way.
For example, if you are using maven, jersey and JSON as media type, you can add the following dependency (and take the provider's registration for granted):
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
Then, get the entity body deserialized using:
Response resp = proxy.serviceMethod("id1");
int status = resp.getStatus();
String statusText = resp.getStatusInfo();
String someHeader = resp.getHeaderString("SOME-HEADER");
YourCustomDTO obj = resp.readEntity(YourCustomDTO.class);
When querying a list of your custom objects (i.e. method returns a JSON array) use the array type to read the body.
Response resp = proxy.serviceMethodThatReturnsCollection();
YourCustomDTO[] obj = resp.readEntity(YourCustomDTO[].class);
Please notice that after reading the body, the stream is closed and trying getEntity()
may throw an exception.
Hope it helps.