<mvc:annotation-driven/>
...
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
                </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            </list>
        </property>
    </bean>

此时配置,response content-type 依然是 text/plain;charset=ISO-8859-1

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg>
                    <util:constant static-field="java.nio.charset.StandardCharsets.UTF_8"/>
                </constructor-arg>
                <property name="writeAcceptCharset" value="false" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

<mvc:annotation-driven> 配置存在时,bean StringHttpMessageConverter 放于 <mvc:message-converters> 中, response 才能正确返回 text/plain;charset=UTF-8


声明 util:
<beans ...
     xmlns:util="http://www.springframework.org/schema/util"
...
 xsi:schemaLocation="...
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-3.1.xsd
...">
xml: 注入静态成员变量至 spring bean
https://www.cnblogs.com/yzx2018/p/8884141.html

标签: none

添加新评论