Spring Boot 使用第三方json解析 : fastjson
2017-11-29 10:34
363 查看
Spring Boot 默认使用的是jackson解析json
导入pom依赖包:
需要在spring boot 的启动类中写一些配置:
方法一:继承以下 的类
方法二:
测试:
在实体类中加入以下注解:
在controller 中进行测试:
页面显示的时间格式是:yyyy-mm-dd HH:mm
导入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.spri 4000 ng.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 中进行测试:
@RestController public 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
相关文章推荐
- SpringBoot 之 第三方Json解析框架FastJson的使用
- (3)spring boot如何使用第三方json解析框架
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Spring Boot 完美使用FastJson解析Json数据
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 【Spring Boot】完美使用FastJson解析JSON数据
- Spring Boot完美使用FastJson解析JSON数据(十三)
- Spring Boot使用FastJson解析JSON数据
- 【spring boot笔记】 springboot 使用第三方json解析
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- spring boot使用fastJson解析数据
- springboot使用第三方的json解析框架fastjson
- (二)初识springboot之使用fastjson解析数据(解决乱码及简单配置)
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
- Spring Boot 学习4--完美使用FastJson解析Json数据
- Spring Boot使用FastJson解析JSON数据的方法
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据
- Springboot使用FastJson后,接口返回中文乱码的问题解决。