Jersey moving from ClientResponse to Response

I am currently using Jersey as a REST proxy to call another RESTful web service. Some calls will be transferred with minimal processing on my server.

Is there any way to do this cleanly? I thought of using a Jersey client to make a REST call, and then convert ClientResponse into a response. Is this possible or is there a better way to do this?

Code example:

@GET @Path("/groups/{ownerID}") @Produces("application/xml") public String getDomainGroups(@PathParam("ownerID") String ownerID) { WebResource r = client.resource(URL_BASE + "/" + URL_GET_GROUPS + "/" + ownerID); String resp = r.get(String.class); return resp; } 

This works if the answer was always successful, but if there is 404 on the other server, I will need to check the response code. In other words, is there a clean way to return the answer I have?

+4
source share
2 answers

As far as I know, there is no convenience method. You can do it:

 public Response getDomainGroups(@PathParam("ownerID") String ownerID) { WebResource r = client.resource(URL_BASE + "/" + URL_GET_GROUPS + "/" + ownerID); ClientResponse resp = r.get(ClientResponse.class); return clientResponseToResponse(resp); } public static Response clientResponseToResponse(ClientResponse r) { // copy the status code ResponseBuilder rb = Response.status(r.getStatus()); // copy all the headers for (Entry<String, List<String>> entry : r.getHeaders().entrySet()) { for (String value : entry.getValue()) { rb.header(entry.getKey(), value); } } // copy the entity rb.entity(r.getEntityInputStream()); // return the response return rb.build(); } 
+5
source

for me the answer is from Martin: JsonMappingException: serializer not found for class sun.net.www.protocol.http.HttpURLConnection $ HttpInputStream Change

 rb.entity(r.getEntityInputStream()); 

to

 rb.entity(r.getEntity(new GenericType<String>(){})); 

helped.

+2
source

Source: https://habr.com/ru/post/1416646/


All Articles