項目使用:sPRing3.0+hibernate
問題描述:使用@ResponseBody 返回json數據時,后臺日志打印正常,前端頁面接收中文亂碼????? ,
解決:
1、如果是新項目可以考慮升級spring為3.1及以上版本,然后 可使用 produces 注解
@RequestMapping(value = "/produces", produces = "application/json"):表示將功能處理方法將生產json格式的數據,此時根據請求頭中的Accept進行匹配,如請求頭“Accept:application/json”時即可匹配;
2、這個是老項目,不想動看原有版本,最終測試可行的方案:
在spring.xml文件中添加:
<!--編碼轉換,其默認為ISO-8859-1--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0" /> <property name="messageConverters"> <list> <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> <property name = "supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
新聞熱點
疑難解答