Skip to content

StringHttpMessageConverter doesn't work under UTF-8 [SPR-8486] #13132

Closed

Description

@spring-projects-issues

Steinway Wu opened SPR-8486 and commented

I found it doesn't work under UTF-8.

After going through the source code of StringHttpMessageConverter, I concludes that although StringHttpMessageConverter has a "DEFAULT_CHARSET" public static member (with default 'ISO-8859-1'), it doesn't offer corresponding setter which leads to the embarrassing that I can't configure StringHttpMessageConverter in applicationContext.xml in a common <property> way.

I suggest that StringHttpMessageConverter should work like FormHttpMessageConverter which contains a private non-static member Charset, and offers corresponding setter for configuration.


Affects: 3.0.5

Issue Links:

Activity

  1. spring-projects-issues commented on Jun 27, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Have you seen the setSupportedMediaTypes(List<MediaType>) method available in the base class AbstractHttpMessageConverter?

  2. spring-projects-issues commented on Jun 27, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Steinway Wu commented

    Yes. However, it seems that *setSupportedMediaTypes* doesn't change the value of *DEFAULT_CHARSET* after looking into *AbstractHttpMessageConverter*. It just set the *AbstractHttpMessageConverter::supportedMediaTypes*. On the other hand, I've found:

    Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET; 
    

    in *StringHttpMessageConverter::writeInternal*, which is called by *AbstractHttpMessageConverter::write*. Then, I'm thinking that my HttpOutputMessage's contentType is not correctly set, otherwise the message converter won't use DEFAULT_CHARSET according to the code above.(I manuallly change it to UTF-8, and it works fine in my project. So I'm sure the DEFAULT_CHARSET is in use other than contentType.getCharSet()). By the way, the message converter is automatically invoked by SpringMVC when I'm returning a string as @ResponseBody in the controller, which means that the content type is mostly like to be automatically set by SpringMVC. So... I'm so sorry that I can't figure out what's wrong indeed...

  3. spring-projects-issues commented on Jun 28, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Then, I'm thinking that my HttpOutputMessage's contentType is not correctly set, otherwise the message converter won't use DEFAULT_CHARSET according to the code above.

    The contentType in the HttpOutputMessage is set depending on the Accept header of the request. If there is no Accept header then the default charset is used.

  4. spring-projects-issues commented on Jun 30, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Steinway Wu commented

    Thank you! Firebug told me that my ACCEPT is */*, that's why it didn't work for me.

  5. spring-projects-issues commented on Apr 22, 2013

    @spring-projects-issues
    CollaboratorAuthor

    blueocci commented

    Same issue in our project.

  6. spring-projects-issues commented on May 7, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    This has actually be done already as part of #14122.

  7. added
    type: bug
    status: duplicate
    in: web
    on Jan 11, 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: duplicate

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions