Spring MVC使用fastjson做消息转换器,与默认Jackson的区别
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
spring mvc支持自定义HttpMessageConverter接收JSON格式的数据,使用fastjson作为消息装换器,只需要在spring的配置文件中加入如下配置代码(需引入fastjson依赖包):
<mvc:annotation-driven>
<!--设置不使用默认的消息转换器-->
<mvc:message-converters register-defaults="false">
<!--配置spring的转换器-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" />
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
<!--配置fastjson中实现HttpMessageConverter接口的转换器-->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4" >
<!--加入支持的媒体类型,返回contentType-->
<property name="supportedMediaTypes">
<list>
<!--这里顺序不能反,一定要先写text/html,不然IE下会出现下载提示-->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
使用fastjson和Jackson的区别如下:
下面的请求代码:
@ResponseBody
@RequestMapping("/testJson")
public Map<String, Object> testJson(String id) {
Map<String, Object> map = new HashMap<>();
Map<String, Object> data = new HashMap<>();
data.put("id", id);
map.put("result", 0);
map.put("message", "成功 success");
map.put("data", data);
return map;
}
访问的时候如果不传id参数,http://localhost:8080/demo/testJson
fastjson返回:
{
"result": 0,
"data": {},
"message": "成功 success"
}
jackson返回:
{
"result": 0,
"data": {
"id": null
},
"message": "成功 success"
}
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献13条内容
所有评论(0)