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

StringHttpMessageConverter should use its default charset [SPR-14821] #19387

Closed
spring-projects-issues opened this issue Oct 19, 2016 · 7 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 19, 2016

Yi opened SPR-14821 and commented

To set @responsebody encoding in spring-webmvc, I used to add the following lines in configuration file:

mvc:annotation-driven
mvc:message-converters
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
</mvc:message-converters>
This override the default charset responsebody handler use. And it worked with spring-mvc version 4.2.7 and below.

However, in the latest version of spring-webmvc(4.3.3), this method does not work. In the new version, StringHttpMessageConverter reads content-type from response header, and if content-type string includes charset information, it use this charset and ignores it's default charset.


Affects: 4.3.1, 4.3.2, 4.3.3

Reference URL: http://stackoverflow.com/questions/40098647/responsebody-encoding-in-spring-mvc-4-3-3

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Yi commented

The error my lies in org.springframework.http.MediaType.getCharSet() method, since I didn't set charset anywhere, this method should return null.

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Hi, to help me to reproduce the issue, could you specify what is the Accept header of the request you send, and what are the Content-Type response headers you receive with Spring MVC 4.2.7 and with Spring MVC 4.3.3 using the configuration you specified in the description? I did a quick try and did not find obvious differences so that would help to identify the use case that produces this behaviour.

@spring-projects-issues
Copy link
Collaborator Author

Yi commented

Some request headers(same under both versions):
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8

Response headers under version 4.2.7:
Content-Type:application/json;charset=UTF-8

Response headers under version 4.3.3:
Content-Type:application/json;charset=ISO-8859-1

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

I can't reproduce the issue, so could you please provide a sample project that demonstrates the problem?

@spring-projects-issues
Copy link
Collaborator Author

Yi commented

I tried to make a sample project, and find that there's something wrong in my mvc configuration.
I didn't add <value>application/json;charset=UTF-8</value> in my configuration.
After I add this, the encoding with version 4.3.3 is correct. I don't know why it works with version 4.2.7, but 4.3.3 is ok.
Proved it's not a bug, thank you.

correct configuration:
mvc:annotation-driven
mvc:message-converters
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>

@spring-projects-issues
Copy link
Collaborator Author

Yi commented

It's the first time I write issue to spring. Should I close this issue? I haven't seen a close button.

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

I have resolved this issue as invalid, thanks for the update.

@spring-projects-issues spring-projects-issues added status: invalid An issue that we don't feel is valid type: regression A bug that is also a regression in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: regression A bug that is also a regression label Jan 12, 2019
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) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants