Spring Boot 使用第三方json解析 : fastjson
来源:互联网 发布:php use require 区别 编辑:程序博客网 时间:2024/05/21 17:08
Spring Boot 默认使用的是jackson解析json
导入pom依赖包:
<!-- 第三方json解析 : fastjson --><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version></dependency>
需要在spring boot 的启动类中写一些配置:
方法一:继承以下 的类
extends WebMvcConfigurerAdapter
@SpringBootApplication@MapperScan("com.spring.boot.mapper") // 扫描该包下相应的class,主要是Mybatis的持久化类。public class SpringBootDemoApplication extends WebMvcConfigurerAdapter{ /** * 方法一: * @param args */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /** * 先定义一个convert 转换消息的对象 * 添加fastJson 的配置信息,比如是否要格式化返回的json 数据 * 在 convert 中添加配置信息 * 将convert 添加到converts 中 */ // 先定义一个convert 转换消息的对象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // 添加fastJson 的配置信息,比如是否要格式化返回的json 数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat ); // 在 convert 中添加配置信息 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); //将convert 添加到converts 中 converters.add(fastJsonHttpMessageConverter); } public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); }}
方法二:
@SpringBootApplication@MapperScan("com.spring.boot.mapper") // 扫描该包下相应的class,主要是Mybatis的持久化类。public class SpringBootDemoApplication{ /** * 方法二 * @param args */ @Bean public HttpMessageConverter fastJsonHttpMessageConverter(){ // 先定义一个convert 转换消息的对象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // 添加fastJson 的配置信息,比如是否要格式化返回的json 数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat ); // 在 convert 中添加配置信息 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastJsonHttpMessageConverter; return converter; } public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); }}
测试:
在实体类中加入以下注解:
@JSONField(format = "yyyy-mm-dd HH:mm")
public class Girl { private String id; private String name; @JSONField(format = "yyyy-mm-dd HH:mm") private Date creatTime; public Girl() { } public Girl(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; }}
在controller 中进行测试:
@RestControllerpublic class HelloController { /** * Spring Boot 默认使用的json解析框架是 jackson * @return */ @RequestMapping("/getDemo") public Girl getDemo(){ Girl girl = new Girl(); girl.setId("25325dfhdfh"); girl.setName("sgdsg"); girl.setCreatTime(new Date()); return girl; }}
页面显示的时间格式是:yyyy-mm-dd HH:mm
阅读全文
0 0
- Spring Boot 使用第三方json解析 : fastjson
- (3)spring boot如何使用第三方json解析框架
- Spring Boot使用FastJson解析JSON数据
- spring boot:使用fastJson解析json数据
- SpringBoot 之 第三方Json解析框架FastJson的使用
- springboot使用第三方的json解析框架fastjson
- 4. Spring Boot 完美使用FastJson解析Json数据
- Spring Boot 学习4--完美使用FastJson解析Json数据
- 【Spring Boot】完美使用FastJson解析JSON数据
- Spring Boot完美使用FastJson解析JSON数据(十三)
- spring-boot实现FastJson解析json数据
- spring boot 配置fastjson解析json
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Spring Boot 使用json框架fastjson
- spring boot使用fastJson解析数据
- 非常棒的第三方JSON解析库Fastjson
- Docker 底层实现
- 停车栈让路栈等候队列完成停车场项目
- Java的string转char*,UTF8字符转码GB2312
- Android Studio 注释模板设置
- ajaxFileupload上传图片到阿里云
- Spring Boot 使用第三方json解析 : fastjson
- 百度信誉V认证影响seo结果么?
- springboot整合mybatis的三种方式
- ZooKeeper的三种角色
- Strust1之配置文件详解
- 在java中,实现scala的Seq转Java的List
- 利用LruCache封装一个自带三级缓存的ImageLoader
- Linux:vim
- 人脸识别之人脸对齐(七)--JDA算法