Skip to content

MappingJackson2XmlHttpMessageConverter should support non UTF charsets #34090

@mrpiggi

Description

@mrpiggi

I currently have to deal with a REST API using text/xml;charset=ISO-8859-1 as MediaType. So just using MappingJackson2XmlHttpMessageConverter seemed obvious to me. Unfortunately I found myself with

RestClientException: No HttpMessageConverter for ... and content type "text/xml;charset=ISO-8859-1"

As it turns out, inheriting from AbstractJackson2HttpMessageConverter checks for hard-coded UTF encodings when calling MappingJackson2XmlHttpMessageConverter.canWrite()

private static final Map<String, JsonEncoding> ENCODINGS;
static {
ENCODINGS = CollectionUtils.newHashMap(JsonEncoding.values().length);
for (JsonEncoding encoding : JsonEncoding.values()) {
ENCODINGS.put(encoding.getJavaName(), encoding);
}
ENCODINGS.put("US-ASCII", JsonEncoding.UTF8);
}

public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
if (!canWrite(mediaType)) {
return false;
}
if (mediaType != null && mediaType.getCharset() != null) {
Charset charset = mediaType.getCharset();
if (!ENCODINGS.containsKey(charset.name())) {
return false;
}
}
ObjectMapper objectMapper = selectObjectMapper(clazz, mediaType);

which is totally reasonable for JSON but not for XML. Overriding this method is not a problem at all but I wanted you to be notified at least.

Metadata

Metadata

Assignees

Labels

for: external-projectNeeds a fix in external projectin: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions