簡述
更詳細内容可參考《連結》
普通java項目(非SpringBoot,maven等)在使用SpringMVC時使用@ResponseBody注解産生的字元串輸出到前段出現中文亂碼。
解決方案
一:Spring配置檔案中配置
<!-- 開啟注解,并解決@request -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
備注:網上提供的方法比較多,但試過後都無功而返,隻有這個成功解決。不過不能否認其他方法的可行性,很有可能時場景不同導緻。
補充:通過上述配置解決了中文亂碼問題,但是在項目調試過程中發現,用工具調試中文顯示正常,用頁面時又出現了亂碼,修改配置如下:
<!-- 開啟注解,并解決@request -->
<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>
</mvc:message-converters>
</mvc:annotation-driven>