Skip to content

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

Closed

Description

@spring-projects-issues

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:

Activity

  1. spring-projects-issues commented on Oct 19, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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.

  2. spring-projects-issues commented on Oct 19, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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.

  3. spring-projects-issues commented on Oct 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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

  4. spring-projects-issues commented on Oct 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sébastien Deleuze commented

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

  5. spring-projects-issues commented on Oct 21, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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>

  6. spring-projects-issues commented on Oct 21, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Yi commented

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

  7. spring-projects-issues commented on Oct 21, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sébastien Deleuze commented

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

  8. added
    status: invalid
    type: regression
    in: web
    on Jan 11, 2019
  9. removed
    type: regression
    on Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: invalid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions