Get RAW-Response from RestTemplate

Viewed 1056

I'm trying to retrieve the raw-reponse from a Spring RestTemplate

String get = restTemplate.postForObject(url, params, String.class);

But since the server responds with Content-Type: Application/Json Spring tries to map the Response to a String-Object by parsing it. Which leads in my case to

Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

since the response contains a json array. Is it possible to get the raw response without parsing? Or do i have to use something other than the RestTemplate?

You can try the problem with these examples

restTemplate.getForEntity("https://api.github.com/users/hadley/orgs", String.class);
restTemplate.getForObject("https://api.github.com/users/hadley/orgs", String.class);
1 Answers

Please try following the code

 @GetMapping(path = "raw")
    public String raw() {
        String response = restTemplate.getForObject("http://jsonplaceholder.typicode.com/users", String.class);
        System.out.println("Length: " + response.length());
        return response;
    }

My RestTemplate looks like this

@Configuration
    public static class Beaner {
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
Related