spring+mybatis 通过@ResponseBody返回结果中文乱码的解决方法

2025-05-29 0 40

问题发生:

通过@Responsebody返回

?

1

2

3

4

5

6

7

8

9
@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;

}

结果:

?

1
{"data":["璧勮璇疯喘绫�","淇冮攢绫�","绠$悊绫�","鏆傛敮绫�","鍌ㄨ繍绫�","璧勪骇绫�","璧勮鏈嶅姟绫�","宸梾绠$悊绫�",<br>"閲囪喘绫�","浜轰簨鑰冨嫟绫�","璐㈠姟绫�","瀹㈡埛绫�","瑙嗘嫇绫�","瀹㈣瘔绫�","浜轰簨娲诲姩绫�","浜轰簨琛屾斂绫�","鍏枃绫�",<br>"璇锋绫�","閿€绠$被","钀ヨ繍涓氬姟绫�"],"status":true}

解决办法:

spring-mvc.xml中配置

?

1

2

3

4

5

6

7

8
<!-- 解决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>

但是配置完后又有了新的问题

?

1
error:...cvc-complex-type.2.1: 元素 'mvc:annotation-driven' 必须不含字符或元素信息项 [子级], 因为该类型的内容类型为空。

后来知道是 springMVC的XSD文件版本不对,换成springMVC 4.0的 XSD就可以了

?

1

2

3

4

5

6

7

8

9

10
<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了

?

1
{"data":["资讯请购类","促销类","管理类","暂支类","储运类","资产类","资讯服务类","差旅管理类","采购类","人事考勤类"<br>,"财务类","客户类","视拓类","客诉类","人事活动类","人事行政类","公文类","请款类","销管类","营运业务类"],"status":true}

?

1
StringHttpMessageConverter 这个方法 有很多转换作用,有时间可以多看看

以上这篇spring+mybatis 通过@ResponseBody返回结果中文乱码的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/sunmao/archive/2017/12/15/8042714.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 spring+mybatis 通过@ResponseBody返回结果中文乱码的解决方法 https://www.kuaiidc.com/113280.html

相关文章

发表评论
暂无评论