java - Access HTTP response when using Jersey client proxy -


i'm using jersey 2.22 consume rest api. approach contract-first, service interface , wrappers used call rest api (using org.glassfish.jersey.client.proxy package).

webclient webclient = clientbuilder.newclient();
webtarget webtarget = webclient.getwebtarget(endpoint);
serviceclass proxy = webresourcefactory.newresource(serviceclass.class, webtarget);

object returnedobject = proxy.servicemethod("id1");

the question is: how underlying http response (http status + body)?
when returnedobject null, need analyze response error message returned example. there way it?
saw can plug filters , interceptors catch response, that's not need.

you should return response result of interface method instead of plain dto.

i'm not sure level of control you're expecting (considering reply @peeskillet comment), response object give opportunity fine tune server's response (headers, cookies, status etc.) , read of them @ client side - might see taking @ response's members getstatus() , getheaders().

the gotcha here how body. this, i'd tell use readentity(class<t>) (not getentity() method 1 might try @ first). long have right media type provider registered, can extract entity dto class in easy way.

for example, if using maven, jersey , json media type, can add following dependency (and take provider's registration granted):

<dependency>     <groupid>org.glassfish.jersey.media</groupid>     <artifactid>jersey-media-json-jackson</artifactid> </dependency> 

then, 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 list of custom objects (i.e. method returns json array) use array type read body.

response resp = proxy.servicemethodthatreturnscollection(); yourcustomdto[] obj = resp.readentity(yourcustomdto[].class); 

please notice after reading body, stream closed , trying getentity() may throw exception.

hope helps.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -