Dec 3rd, 2012, 10:12 AM
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();
HttpEntity<Object> httpEntity = new HttpEntity<Object>(message.getPayload(), headers);
where my message.payload has value
<?xml version="1.0" encoding="utf-8"?>
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.
Dec 10th, 2012, 01:53 AM
The message converter that was used was StringHttpMessageConverter where the default charset is
StringHttpMessageConverter DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
MediaType mediaType = new MediaType("application", "xml", Charset.forName("UTF-8"));
resolved the issue.
Earlier the code was
MediaType mediaType = MediaType.APPLICATION_XML;
The charset was null in this case so the DEFAULT_CHARSET = Charset.forName("ISO-8859-1") was being picked up by the StringHttpMessageConverter.
Dec 19th, 2012, 05:36 AM
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.
Dec 21st, 2012, 04:11 AM
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.
Tags for this Thread