Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MappingJackson2XmlHttpMessageConverter should support non UTF charsets #34090

Open
mrpiggi opened this issue Dec 13, 2024 · 0 comments
Open

MappingJackson2XmlHttpMessageConverter should support non UTF charsets #34090

mrpiggi opened this issue Dec 13, 2024 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@mrpiggi
Copy link

mrpiggi commented Dec 13, 2024

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 13, 2024
@sdeleuze sdeleuze self-assigned this Dec 16, 2024
@sdeleuze sdeleuze added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 16, 2024
@sdeleuze sdeleuze added this to the 6.2.2 milestone Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants