PDA

View Full Version : Encoding problem



ptashcka
May 15th, 2011, 07:58 AM
Hi.
Both twitter and facebook use default RestTemplate object configured with FormHttpMessageConverter. The default encoding for this converter is set to "ISO-8859-1", though UTF-8 should be used with Facebook and Twitter API.

Looks like a bug. I've overridden template constructors as a workaround:


List<HttpMessageConverter<?>> converters = getRestTemplate().getMessageConverters();
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof FormHttpMessageConverter) {
((FormHttpMessageConverter) converter).setCharset(Charset.forName("UTF8"));
}
if(converter instanceof MappingJacksonHttpMessageConverter) {
MappingJacksonHttpMessageConverter jsonConverter = (MappingJacksonHttpMessageConverter) converter;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new FacebookModule());
objectMapper.configure(DeserializationConfig.Featu re.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
}
}

probably there is a better solution?

Keith Donald
May 15th, 2011, 09:36 AM
We should definitely fix this issue if its a bug. What specific problems were occurring in your application that led you to notice this issue? Just curious.

ptashcka
May 15th, 2011, 09:47 AM
I use russian to post to Facebook and Twitter so my messages turned into something like:
?? ?????? hello world :)
Specifically for these methods:
facebookConnection.getApi().feedOperations().postL ink();
timelineOperations.updateStatus();

habuma
May 16th, 2011, 09:29 AM
It looks like this is a bug when used with Spring 3.0.x. In Spring 3.1, UTF-8 is FormHttpMessageConverter's default character set, so it works fine. But in Spring 3.0.x, it defaults to ISO-8859-1, which is where you're probably seeing the problem.

This was corrected in SPR-7789, but that fix doesn't appear in any released 3.0.x build.

I've created https://jira.springsource.org/browse/SOCIAL-170 to address this in Spring Social.