Skip to content

Jackson message converters and codecs do not respect character encoding in canRead/canWrite #25076

Closed
@saimonsez

Description

@saimonsez

Affects: org.springframework:spring-web:5.1.15.RELEASE


XML webservices seem to be limited to unicode encodings. Using jackson-dataformat-xml 2.9.8 for a webservice which produces "text/xml;charset=ISO-8859-1" outputs utf-8.

The problem might be located in AbstractJackson2HttpMessageConverter:

...
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
...

JsonEncoding only provides unicode. A workaround would be to override the whole method but it is quite lenghty.

I made a very small reproducer: https://github.com/saimonsez/spring-jackson-xml-encoding-problem

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions