Closed
Description
When the issue of charset being ignored #25076 was resolved on 5.2.7/5.2.8 the problem now is the opposite. I have lot of clients that are sending ;charset=ISO-8859-1 on their request (for example by using RestTemplate is added by default), and know request and responses in Japanese language are garbled (should be UTF-8)
A simple way to ignore the charset by configuration or at least making some members protected would be nice.
Now the only alternative is to override complete methods
public class ForcedUTF8MappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
public ForcedUTF8MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper);
}
@Override
public Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
JavaType javaType = getJavaType(type, contextClass);
return readJavaType(javaType, inputMessage);
}
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) throws IOException {
Charset charset = StandardCharsets.UTF_8;
boolean isUnicode = true;
try {
if (inputMessage instanceof MappingJacksonInputMessage) {
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
if (deserializationView != null) {
ObjectReader objectReader = this.objectMapper.readerWithView(deserializationView).forType(javaType);
if (isUnicode) {
return objectReader.readValue(inputMessage.getBody());
}
else {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
return objectReader.readValue(reader);
}
}
}
if (isUnicode) {
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
}
else {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
return this.objectMapper.readValue(reader, javaType);
}
}
catch (InvalidDefinitionException ex) {
throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex);
}
catch (JsonProcessingException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex, inputMessage);
}
}
@Override
protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) {
return JsonEncoding.UTF8;
}
}