'Rest template.exchange returns response as junk while doing a GET request to a server running REST service in dot net core

}

Even i have setted the stringHttpMessageConverter to charset utf-8 but didnt work.

My code like

//restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-32")));
         HttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-16"));
            List<HttpMessageConverter<?>> httpMessageConverter = new ArrayList();
            httpMessageConverter.add(stringHttpMessageConverter);
            restTemplate.setMessageConverters(httpMessageConverter);

        responseEntity = restTemplate.exchange(/*new URI(*/targetUrl/*)*/, httpMethod, reqEntity, String.class);

If you look at the response, it looks like an chinese chars..



Solution 1:[1]

You can add this to your headers

httpHeaders.set("Accept-Charset", StandardCharsets.UTF_8.name());

Solution 2:[2]

You can try below code

responseEntity = restTemplate.exchange(/*new URI(*/targetUrl/*)*/, httpMethod, reqEntity, Object.class);

Instead of String.class When i used Object.class then its start working for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcello B.
Solution 2