I am using the following code to consume a rest post service
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
As expected I am getting error. and status code 400 Bad Request
. But i am not getting the error message. when i run response.getStatusInfo()
i get bad request but my server sends additional info.
when i call this using postman
i get error info in the Body window.
So how do I get this error body info from the response object? or any other way???
You get the response body as usual with Response#readEntity:
Read the message entity input stream as an instance of specified Java type using a
MessageBodyReader
that supports mapping the message entity stream onto the requested type.
Example:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
String body = response.readEntity(String.class);
With Response#getStatusInfo you get only HTTP status, class and reason phrase.