I am using Rest client of Firefox. I want to get value from response
that is showing on Response body(Raw)
in Rest-Client. I want to get this value in SpringBoot
. Is it possible? If yes then How?
I have tried too many times but didn't get Satisfactory solution.
1 Answer
Using a Spring RestTemplate to make the calls will return a ResponseEntity. The simplest way to get the raw response would be this:
RestTemplate restTemplate = new RestTemplate();
try{
ResponseEntity<String> response = restTemplate.getForEntity(URI.create("http://example.org"),String.class);
System.out.println(response.getBody());
} catch (RestClientResponseException exception){
System.out.println(String.format("Error code %d : %s",e.getStatusCode().value(),e.getResponseBodyAsString()));
HttpHeaders errorHeaders = e.getResponseHeaders();
}
The ResponseEntity class will allow you to access the headers as well.
For more information on RestTemplate you can look at the docs here.
-
Thanks @Michael... But in case of success it works fine but when I got error then I can't get anything from response body....– SandeshCommented Jul 25, 2017 at 7:31
-
I've updated the response to include access to the body on an error. If this gives you what you need, please mark the answer as accepted to help others in the future. Commented Jul 25, 2017 at 14:49