Responsebody encoding in Spring MVC 4.3.3

To set @responsebody encoding in spring -webmvc, I used the following lines to add to the 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 overrides the default charset response handler handler. 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 the type of content from the response header, and if the content line contains encoding information, it uses this encoding and ignores the default encoding.

I know that 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 this on every method or on every controller. Is there a way to set the response encoding globally as before?

+4
1

, <value>application/json;charset=UTF-8</value>. , 4.2.7 , 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>

+2

Source: https://habr.com/ru/post/1658046/


All Articles