2
RestTemplate restTemplate = new RestTemplate(); 
String response = restTemplate.getForObject(link, String.class);
JSONObject json = (JSONObject) new JSONParser().parse(response);

json in url: {"description": "Explains how to construct 45° and 90°"}

json I'm getting: {"description": "Explains how to construct 45° and 90°"}

any help to get actual json, without weird characters is apreciated.

G.Brown
  • 339
  • 3
  • 15

1 Answers1

3

You just need to add the StringHttpMessageConverter to the template's message converters:

    RestTemplate restTemplate = new RestTemplate(); 
String response = restTemplate.getForObject(link, String.class);
    restTemplate.getMessageConverters()
            .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
JSONObject json = (JSONObject) new JSONParser().parse(response);

Here main code is

restTemplate.getMessageConverters()
                .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

It will convert message as UTF-8

Imports:

import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;

For details code, check this my answer . It has full code

Hope this will solve your problem

Thanks :)

Md. Sajedul Karim
  • 7,031
  • 3
  • 56
  • 81
  • 1
    can you please recheck the code? cos' I'm getting compilation error. may be you meant ` restTemplate.getMessageConverters() .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));` its not working. As I'm still getting weirdos in the JSON – G.Brown Mar 18 '19 at 08:37
  • 1
    I tried the same code. Somehow, UTF-8 seems to not work – G.Brown Mar 18 '19 at 08:45
  • For rest call please check my this answer. https://stackoverflow.com/a/51805956/3073945 – Md. Sajedul Karim Mar 18 '19 at 09:21
  • I saw it, but my issue is also different. As I'm getting weird characters in 2nd step itself. String response = restTemplate.getForObject(link, String.class); – G.Brown Mar 19 '19 at 13:03
  • 1
    I saw your code. Just in the second step, where data is retrieved from url, is not working – G.Brown Mar 20 '19 at 05:27