Announcement Announcement Module
Collapse
No announcement yet.
RestTemplate is changing the UTF-8 charater to junk value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate is changing the UTF-8 charater to junk value

    I'm using the rest template method

    public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType)

    where i have created HttpEntity using

    HttpHeaders httpHeaders=new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_XML);

    HttpEntity<Object> httpEntity = new HttpEntity<Object>(message.getPayload(), headers);

    where my message.payload has value

    <?xml version="1.0" encoding="utf-8"?>
    <nickName>¥£</nickName>

    However when the REST Service receives ¥£ this value it is turned into some junk value.

    Is there something that needs to be done when calling the service using RestTemplate to ensure that the UTF-8 encoding is supported while parsing the xml values in the HttpEntity.

  • #2
    Resolved

    The message converter that was used was StringHttpMessageConverter where the default charset is

    StringHttpMessageConverter DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

    Setting

    MediaType mediaType = new MediaType("application", "xml", Charset.forName("UTF-8"));
    httpHeaders.setContentType(mediaType);

    resolved the issue.

    Earlier the code was

    MediaType mediaType = MediaType.APPLICATION_XML;
    httpHeaders.setContentType(mediaType);

    The charset was null in this case so the DEFAULT_CHARSET = Charset.forName("ISO-8859-1") was being picked up by the StringHttpMessageConverter.

    Comment


    • #3
      ust when you think you've got your spring web application nicely under control your first customer from a Scandinavian country tries to place an order. And then you are hit by the evil character encoding monster. Your customer doesn't live in København but in K�benhavn and their last name is now MÃ¥rtensson instead of Mårtensson. Chances are your customers from China will be treated even worse by your web app.

      Comment


      • #4
        Thanks Zeeshan for pointing out the issues that one can face. So what do you suggest should be a solution that would allow me to handle any such issue in future. I beleive one way is to check the locale and get set the encoding based on it.

        Comment

        Working...
        X