您好,登錄后才能下訂單哦!
問題發生:
通過@Responsebody返回
@ResponseBody @RequestMapping(value ="/selectByFormId",method = RequestMethod.GET) public Map<String,Object> getClassName(String formId){ List<String> list =formInfoService.selectClassName(formId); Map<String,Object> map = new HashMap<String, Object>(); map.put("data", list); map.put("status", true); return map; }
結果:
{"data":["璧勮璇瘋喘綾�","淇冮攢綾�","綆$悊綾�","鏆傛敮綾�","鍌ㄨ繍綾�","璧勪駭綾�","璧勮鏈嶅姟綾�","宸梾綆$悊綾�",<br>"閲囪喘綾�","浜轟簨鑰冨嫟綾�","璐㈠姟綾�","瀹㈡埛綾�","瑙嗘嫇綾�","瀹㈣瘔綾�","浜轟簨媧誨姩綾�","浜轟簨琛屾斂綾�","鍏枃綾�",<br>"璇鋒綾�","閿?綆$被","钀ヨ繍涓氬姟綾�"],"status":true}
解決辦法:
在spring-mvc.xml中配置
<!-- 解決ResponseBody 亂碼問題StringHttpMessageConverter這個轉換器轉換時,父類AbstractHttpMessageConverter的supportedMediaTypes屬性默認是iso-8895-1導致 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=utf-8"></property> </bean> </mvc:message-converters> </mvc:annotation-driven>
但是配置完后又有了新的問題
error:...cvc-complex-type.2.1: 元素 'mvc:annotation-driven' 必須不含字符或元素信息項 [子級], 因為該類型的內容類型為空。
后來知道是 springMVC的XSD文件版本不對,換成springMVC 4.0的 XSD就可以了
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
然后就ok了
{"data":["資訊請購類","促銷類","管理類","暫支類","儲運類","資產類","資訊服務類","差旅管理類","采購類","人事考勤類"<br>,"財務類","客戶類","視拓類","客訴類","人事活動類","人事行政類","公文類","請款類","銷管類","營運業務類"],"status":true}
StringHttpMessageConverter 這個方法 有很多轉換作用,有時間可以多看看
以上這篇spring+mybatis 通過@ResponseBody返回結果中文亂碼的解決方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。