How can I get the raw json string from spring rest template? I have tried following code but it returns me json without quotes which causes other issues, how can i get the json as is.
ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class); String json = response.getBody().toString();
To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.
For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.
You don't even need ResponseEntity
s! Just use getForObject
with a String.class
like:
final RestTemplate restTemplate = new RestTemplate(); final String response = restTemplate.getForObject("https://httpbin.org/ip", String.class); System.out.println(response);
It will print something like:
{ "origin": "1.2.3.4" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With