Java Spring resttemplate character encoding for query param

Viewed 975

I am using Java Spring Resttemplate to get json via get request. Query parameter values can contain Russian characters. In such cases, I do not receive data from the API.

My code:

URI uri = UriComponentsBuilder.fromHttpUrl(url)
        .path("/resume")
        .queryParam("text", "Java-разработчик")
        .build()
        .toUri();

log.info("URI: " + uri);

logs:

https://api.link.com/resume?text=Java-%D0%A1%D0%82%D0%A0%C2%B0%D0%A0%C2%B7%D0%A1%D0%82%D0%A0%C2%B0%D0%A0%C2%B1%D0%A0%D1%95%D0%A1%E2%80%9A%D0%A1%E2%80%A1%D0%A0%D1%91%D0%A0%D1%94

I also tried this, but it didn't help:

1) 
restTemplate.getMessageConverters()
        .add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
2) 
URI uri = UriComponentsBuilder.fromHttpUrl(url)
        .path("/resume")
        .queryParam("text", URLEncoder.encode("Java-разработчик", HTTP.UTF-8))
        .build()
        .toUri();

enter image description here

What can be done so that Russian characters do not change and the request is sent directly with them?

That is, in the end, the request should look like this:

https://api.link.com/resume?text=Java-разработчик
1 Answers

The problem turned out to be in the encoding. Installing UTF-8 was helped only by using encode(StandardCharsets.UTF_8) for UriComponentsBuilder

URI uri = UriComponentsBuilder.fromHttpUrl(url)
        .path("/resume")
        .queryParam("text", "Java-разработчик")
        .encode(StandardCharsets.UTF_8)
        .build()
        .toUri();

Why queryParam("text", URLEncoder.encode(myString, HTTP.UTF-8)) did not work remains a big mystery.

Related