Spring Boot 之json数据解析
来源:互联网 发布:单页网站怎么优化 编辑:程序博客网 时间:2024/05/18 14:26
Spring Boot在解析json时,默认使用的是jackson,所以在对json进行处理时,是非常方便的。以下是具体的步骤:
第一步:在pom.xml中添加fastjson依赖
<!-- 添加fastjson依赖包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
第二步:创建Demo实体类,生成getXxx()和setXxx()方法
第三步:编写Controller层代码
/**
* Spring Boot默认使用的json解析框架是jackson
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo() {
Demo demo = new Demo();
demo.setId(1);
demo.setName("Steven");
demo.setCreateTime(new Date());
demo.setRemark("这是备注信息");
returndemo;
}
第四步:以下两种方式,任选其一,在启动类中实现即可:
4.1、继承WebMvcConfigurerAdapter类,重写configureMessageConverters()方法
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>>converters) {
super.configureMessageConverters(converters);
//1、需要先定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig =new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息;
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、讲convert添加到converts当中。
converters.add(fastConverter);
}
4.2、使用@Bean注入fastJsonHttpMessageConverter
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig =new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);;
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
各位大神,请多多点评!!!
- Spring Boot 之json数据解析
- spring-boot实现FastJson解析json数据
- Spring Boot使用FastJson解析JSON数据
- spring boot:使用fastJson解析json数据
- 3.Spring boot之返回json数据
- spring boot起步之返回json数据
- Spring Boot之返回JSON数据
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 4. Spring Boot 完美使用FastJson解析Json数据
- Spring Boot 学习4--完美使用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数据
- spring boot 配置fastjson解析json
- 2. Spring Boot返回json数据【从零开始学Spring Boot】
- python多线程+pymysql
- Oracle操作语句之DDL语句
- 微信第三方授权核心
- 下面哪些调用转换支持可变长度参数
- C#代码生成XML文件
- Spring Boot 之json数据解析
- C++ Socket
- SQL总结
- SpringMvc MappingJacksonHttpMessageConverter 异常
- STM32CUBEMX_STM32F0_TIM17
- [BZOJ1516][VijosP1061]迎春舞会之三人组舞
- 深入理解 Android 之 View 的绘制流程(二)_Measure
- ORA-12516 "TNS监听程序找不到符合协议堆栈要求的可用处理程序" 解决方案
- 分布式事务