-2

I'm using springframefork. I do get-request, but in server answer's header is not encoding information, but I know that it use UTF-8. How can I force encode answer body in UTF-8? This is not a web application - this is a simple java application.

public static void main(String[] args) {
    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Authorization", "bearer ********");
    HttpEntity payload = new HttpEntity(headers);

    ResponseEntity<String> response = restTemplate.exchange("http://*******", HttpMethod.GET, payload, String.class);
    log.debug(response);
}

response is like

response = (org.springframework.http.ResponseEntity) <200 OK,{"id":228804,"field1":"ТеÑÑовÑй обÑекÑ","field2":"ТеÑÑÐ¾Ð²Ð°Ñ Ð¿ÑиÑина","field3":"ТеÑÑÐ¾Ð²Ð°Ñ Ð¿ÑиÑина","field4":221,"field5":null,"field6":"г.СанкÑ-ÐеÑеÑбÑÑг, 15-Ñ Ð»Ð¸Ð½Ð¸Ñ Ð.Ð., дом

etc

  • 1
    Possible duplicate of [Spring MVC UTF-8 Encoding](http://stackoverflow.com/questions/5928046/spring-mvc-utf-8-encoding) – bbop99 Mar 03 '17 at 10:35
  • Can you share some code or better detail your question? It's not clear to me if you wrote the controller method, or if you are just trying to consume the service, or if you have control to the whole project and can modify web.xml for example like @Antonvb suggested – amicoderozer Mar 03 '17 at 10:41
  • I just edited question. I hope it helps. – Иван Васильев Mar 03 '17 at 11:54

2 Answers2

2

If you can edit web.xml, check this answer -> https://stackoverflow.com/a/5928162/6822933

Adding the CharacterEncodingFilter filter should solve your problem.

Community
  • 1
  • 1
bbop99
  • 1,625
  • 1
  • 11
  • 25
1
public static String strDecode(String str) throws UnsupportedEncodingException, CharacterCodingException {
    CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
    byte sByte[] = str.getBytes("iso-8859-1");
    ByteBuffer isoBuf = ByteBuffer.wrap(sByte);
    return utf8Decoder.decode(isoBuf).toString();
}