這確實是個蛋疼的問題,Spring中解析字符串的轉換器默認編碼居然是ISO-8859-1
既然找到問題了,那就必須想辦法改過來,不同版本的Spring好像方法還不一樣,網上不少說的都是Spring3.*的,現在Spring4早都出來了
更改方式可以參考
http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody
http://www.cnblogs.com/chenying99/archive/2012/04/17/2453017.html
我現在用的Spring4.2.5,上面說的幾個方法都試了,最后發現只有這兩個可以
方法一,使用(produces = "application/json; charset=utf-8"):
@RequestMapping(value="/getUsersByPage",produces = "application/json; charset=utf-8") // @RequestMapping("/getUsersByPage") @ResponseBody public String getUsersByPage(String page,String rows,String text,HttpServletRequest request,HttpServletResponse response){
方法二,在spring-mvc.xml中添加:
<!-- 處理請求返回json字符串的中文亂碼問題 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
以上兩種方式經過驗證都沒有問題。