7

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.

I know I can write like this to solve this problem:

@RequestMapping(value = "/getDealers", method = RequestMethod.GET, 
produces = "application/json; charset=utf-8")
@ResponseBody
public String sendMobileData() {

}

But I have to write it on every method or every controller. Is there any way to set responsebody encoding globally like I did before?

易天明
  • 1,461
  • 1
  • 10
  • 10
  • you can shorten it a little bit by using '@RestController' instead '@Controller' and also '@ResponseBody' wont be needed then.. but I guess 'produces' may be still required – hi_my_name_is Oct 18 '16 at 05:45
  • seems you have to use AnnotationMethodHandlerAdapter, answer explains it does not work with , not sure why http://stackoverflow.com/a/3617594/410677 – kuhajeyan Oct 18 '16 at 06:33
  • as an ugly hack, a filter may be used here to modify the response headers. – John Donn Oct 18 '16 at 07:01
  • @ 易天明, Did you set force flag in your CharacterEncodingFilter? – eg04lt3r Oct 18 '16 at 15:06
  • @John Donn, @ eg04lt3r CharacterEncodingFilter does not help, I forgot to set force flag before, but even i set force flag to true, the encoding is still wrong. – 易天明 Oct 19 '16 at 04:13
  • I remember that characterEncodingFilter does not affect @responsebody response. – 易天明 Oct 19 '16 at 04:29
  • You could try to use a tool like Fiddler to inspect the response headers you get when putting **produces = "application/json; charset=utf-8"** on a method. and write your own filter, if needed, to make the response headers for all methods (which produce JSON) similar. But, as I said, this is a hack (a nonstandard way to resolve a problem, which might create difficulties in maintenance). – John Donn Oct 19 '16 at 05:53
  • In older version, if I don't set produces, StringHttpMessageConverter will use it's default charset. Maybe I should write an issue to spring. – 易天明 Oct 19 '16 at 10:32
  • Maybe the error lies in org.springframework.http.MediaType class. Since I didn't set charset anywhere, it's #getCharSet() method should return null. – 易天明 Oct 19 '16 at 10:52

1 Answers1

3

I find that I didn't add <value>application/json;charset=UTF-8</value> in my configuration. I don't know why my old configuration works with version 4.2.7 and below, but this new configuration just works with version 4.3.3:

<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>

易天明
  • 1,461
  • 1
  • 10
  • 10