作者:小米-发烧友 | 来源:互联网 | 2023-10-10 16:22
1.SpringMVC集成fastjson在SpringMVC中集成fastjson,集成之后,注解ResponseBody返回的对象就能够自动解析成json数据返
1. SpringMVC 集成fastjson
在SpringMVC中集成fastjson, 集成之后,注解 @ResponseBody 返回的对象就能够自动解析成 json数据返回。
fastjson 的效率要高于 jackson、gson
-
下载 fastjson.jar
-
配置 springmvc.xml 文件
application/json;charset=UTF-8
- 添加注解 @ResponseBody
@RequestMapping(value = "/ajax")public @ResponseBodyCustomer ajax(Integer id, HttpServletResponse response) {Customer customer = customerService.queryCustomerById(id);response.setContentType("text/html;charset=utf-8");return customer;}
返回的数据格式:
{"cust_address": "北京三里桥","cust_createtime": 1460104321000,"cust_id": 16,"cust_industry": "2","cust_level": "22","cust_linkman": "马化腾","cust_mobile": "13888888888","cust_name": "刘强东","cust_phone": "0108888887","cust_source": "6","cust_zipcode": "123456"
}
2. 配置 fastjson 后,返回字符串时会在外面强制添加双引号
例如:
- 如果返回的字符串为
哈哈
, 最终返回得到的数据是 "哈哈"
- 返回的字符串中包含双引号,例如
{"name":"ethan"}
,则 fastjson处理后,最终返回的结果是 "{\"name\":\"ethan\"}"
要想得到原始字符串,解决办法:
添加配置字符串转换器StringHttpMessageConverter
...