當使用@ResponseBody返回的字符串帶有中文時,返回類型為String會被StringHttpMessageConverter處理,當時查看源碼發現默認的Charset DEFAULT_CHARSET使用的是ISO-8859-1。
context.xml里有配置如下信息時候,但是發現沒有生效。需要把這段配置在<mvc:annotation-driven/>注解前面!
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" value="#{jsonUtil.objectMapper}"/>
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<mvc:annotation-driven/> //這行配置在后面上面的配置才會生效,spring啟動的時候這行注解做的事情會初始化各種配置,包括上面的配置所做的事情,后面再注入到spring是不生效的!