1

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.

enter image description here

1 Answer 1

3

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.

2
  • Thanks @Michael... But in case of success it works fine but when I got error then I can't get anything from response body....
    – Sandesh
    Commented 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.