I am using RestTemplate
to make an HTTP call to our service which returns a simple JSON response. I don't need to parse that JSON at all. I just need to return whatever I am getting back from that service.
So I am mapping that to String.class
and returning the actual JSON response
as a string.
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url, String.class);
return response;
Now the question is -
I am trying to extract HTTP Status codes
after hitting the URL. How can I extract HTTP Status code from the above code? Do I need to make any change into that in the way I doing it currently?
Update:-
This is what I have tried and I am able to get the response back and status code as well. But do I always need to set HttpHeaders
and Entity
object like below I am doing it?
RestTemplate restTemplate = new RestTemplate();
//and do I need this JSON media type for my use case?
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//set my entity
HttpEntity<Object> entity = new HttpEntity<Object>(headers);
ResponseEntity<String> out = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
System.out.println(out.getBody());
System.out.println(out.getStatusCode());
Couple of question - Do I need to have MediaType.APPLICATION_JSON
as I am just making a call to url which returns a response back, it can return either JSON or XML or simple string.
If you don´t want to leave the nice abstraction around
RestTemplate.get/postForObject...
methods behind like me and dislike to fiddle around with the boilerplate stuff needed when usingRestTemplate.exchange...
(Request- and ResponseEntity, HttpHeaders, etc), there´s another option to gain access to the HttpStatus codes.Just surround the usual
RestTemplate.get/postForObject...
with a try/catch fororg.springframework.web.client.HttpClientErrorException
andorg.springframework.web.client.HttpServerErrorException
, like in this example:The
org.springframework.web.client.HttpServerErrorException
is added here for the errors with a50x
.Now you´re able to simple react to all the StatusCodes you want - except the appropriate one, that matches your HTTP method - like
GET
and200
, which won´t be handled as exception, as it is the matching one. But this should be straight forward, if you´re implementing/consuming RESTful services :)response contains 'body', 'headers' and 'statusCode'
to get statusCode : response.getStatusCode();
exchange(...) works but if you want less code, you can use
which returns an Entity containing StatusCode. Change your example code to this:
To test it you can use this snippet from my unit test:
There can be some slightly trickier use cases someone might fall in (as I did). Consider the following:
Supporting a Page object in order to use it with RestTemplate and ParameterizedTypeReference:
RestPageResponse:
Using
ParameterizedTypeReference
will yield the following:Calling #exchange:
Now here is the "tricky" part.
Trying to call exchange's
getStatusCode
will be impossible because the compiler, unfortunately, will be unaware of the "intended" type ofresponse
.((ResponseEntity<RestResponsePage<MyObject>>) response).getStatusCode()
In this case, you have to explicitly cast the variable to the desired Class to get the
statusCode
(and/or other attributes)!Use the
RestTemplate#exchange(..)
methods that return aResponseEntity
. This gives you access to the status line and headers (and the body obviously).