1.搭建环境(可以参考前面的springboot入门)
maven
sts
java8
2.在pom.xml中引入fastjson对应的坐标(注意:version最好是1.2以上的)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
3.编写解析返回json数据的方法
方法一:启动类继承extends WebMvcConfigurerAdapter
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter{
/**
* 重写转换方法
* @author yimeidaoren77
*/
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.创建一个convert消息转换对象
FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
//2.创建一个fastJson的配置对象,然后配置格式化信息
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.在convert中添加配置信息
fastConvert.setFastJsonConfig(config);
//4.将convert添加到converts里面
converters.add(fastConvert);
}
public class App extends WebMvcConfigurerAdapter{
/**
* 重写转换方法
* @author yimeidaoren77
*/
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.创建一个convert消息转换对象
FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
//2.创建一个fastJson的配置对象,然后配置格式化信息
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.在convert中添加配置信息
fastConvert.setFastJsonConfig(config);
//4.将convert添加到converts里面
converters.add(fastConvert);
}
方法二:覆盖方法configureMessageConverters
public HttpMessageConverters fastJsonHttpMessageConverters(){
//1.创建一个convert消息转换对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.创建一个fastJson的配置对象,然后配置格式化信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converters = fastConverter;
return new HttpMessageConverters(converters);
}
//1.创建一个convert消息转换对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.创建一个fastJson的配置对象,然后配置格式化信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converters = fastConverter;
return new HttpMessageConverters(converters);
}
4.编写pojo类
public class City { private Long id; private Long provinceId; private String cityName; private String description; @JSONField(format="yyyy-MM-dd HH:mm:ss") private Date createTime; ... }
4.测试
其中遇到了如下问题:
错误原因如下:
后来查了下资料找到了.pom文件中添加依赖pom的解释:
http://blog.csdn.net/yao123long/article/details/49925659
相关链接
fastJson的@JSonField注解说明
https://www.w3cschool.cn/fastjson/fastjson-jsonfield.html
相关推荐
SpringBoot中使用FastJson解析Json数据示例代码
本篇文章主要介绍了springboot实现FastJson解析json数据的方法,非常具有实用价值,需要的朋友可以参考下
主要介绍了SpringBoot如何使用Fastjson解析Json数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
fastjson解析json数据,解析速度快
使用fastjson.jar解析复杂的json数据,包含完整实例代码
FastJson解析Json数据源码,
fastjson 快速解析json json、List互转
java使用fastjson解析json字符串,附fastjson包,后台接口需输出json格式字符串,非xml格式
使用org.json 、谷歌gson 以及阿里巴巴fastjson解析JSON格式数据的完整例子(JavaWeb项目)
阿里巴巴 fastjson是目前解析json数据的一种比较高效,快速的方式。
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
详细Gson和fastJson解析json案例
二、通过fastjson解析json数据 一、原始数据内容eventLogJson.txt { “u”: { “cookieid”: “HsOorABPB”, “account”: “05289”, “email”: “Fh8h@G4hbi.com”, “phoneNbr”: “20096655112”, “birthday”...
本篇文章主要介绍了Spring Boot使用FastJson解析JSON数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
实现json转换,fastjson json json转换,可以帮助开发中涉及json格式处理的程序猿。
本篇文章主要介绍了java使用FastJson解析Json数据,fastjson 是一个性能极好的用 Java 语言实现的 JSON 解析器和生成器,有兴趣的可以了解一下。
springboot 使用fastjson自定义序列化和反序列化
json解析在android开发中是非常,常见的fastjson是阿里巴巴的一个库,对于json开发效率很高,我写了一个简单例子
Volley。fastJson解析网络Json ,多线程显示图片,简单缓存图片,万能适配器,完美解决图片排序混乱问题,完美解决图片多次加载问题
在本篇文章里小编给大家整理的是一篇关于springboot中用fastjson处理返回值问题详解内容,需要的朋友们参考下。