SpringBoot中使用FastJson解析Json数据
场景
1.SpringBoot默认配置的是Jackson。
2.项目搭建专栏:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/35688
实现
引入fastJson的依赖
<!-- fastjson的依赖 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency>
实现方式1
通过继承WebMvcConfigurerAdapter来实现
找到springBoot的启动类
package com.example.demo;import java.util.List;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service"})
public class HelloSpringBootApplication extends WebMvcConfigurerAdapter{@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {//创建FastJson的消息转换器FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();//创建FastJson的配置对象FastJsonConfig config = new FastJsonConfig();//对Json数据进行格式化config.setSerializerFeatures(SerializerFeature.PrettyFormat);convert.setFastJsonConfig(config);converters.add(convert);}public static void main(String[] args) {SpringApplication.run(HelloSpringBootApplication.class, args);}}
通过extends WebMvcConfigurerAdapter并重写configureMessageConverters方法来实现。
新建Controller
package com.example.demo.controller;import java.util.Date;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import com.example.demo.pojo.User;@Controller
public class TestFastjsonController {@ResponseBody@RequestMapping("/testFastJson")public Object show() {User user =new User();user.setId(1);user.setUsername("霸道");user.setPassword("流氓气质");user.setDate(new Date());return user;}
}
新建pojo
user.javapackage com.example.demo.pojo;import java.util.Date;import com.alibaba.fastjson.annotation.JSONField;public class User {private Integer id;private String username;private String password;@JSONField(format="yyyy-MM-dd")private Date date;public Integer getId() {return id;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
运行看效果
实现方式2
在项目启动类中
package com.example.demo;import java.util.List;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service"})
public class HelloSpringBootApplication
{@Beanpublic HttpMessageConverters fastJsonMessageConverter() {//创建FastJson的消息转换器FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();//创建FastJson的配置对象FastJsonConfig config = new FastJsonConfig();//对Json数据进行格式化config.setSerializerFeatures(SerializerFeature.PrettyFormat);convert.setFastJsonConfig(config);HttpMessageConverter<?> con =convert;return new HttpMessageConverters(con);}public static void main(String[] args) {SpringApplication.run(HelloSpringBootApplication.class, args);}}
同样运行效果
源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11074485
SpringBoot中使用FastJson解析Json数据相关推荐
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- fastjson解析JSON数据乱序导致的问题
本文作者:合肥工业大学 电子商务研究所 钱洋 email:1563178220@qq.com . 内容可能有不到之处,欢迎交流. 未经本人允许禁止转载. 文章目录 问题背景 问题解决 完整的案例代码 ...
- 5.spring boot使用FastJson解析JSON数据
2019独角兽企业重金招聘Python工程师标准>>> 1.引入FastJson依赖包 <dependency><groupId>com.alibaba< ...
- Andorid中使用Gson和Fast-json解析库解析JSON数据---第三方库学习笔记(二)
JSON介绍: JSON:JavaScript对象表示法 JSON是存储和交换文本信息的语法. 特点: JSON是轻量级的文本数据交换格式 JSON独立于语言和平台 JSON具有自我描述性,更易理解 ...
- Gson解析JSON数据中动态未知字段key的方法
转载自:https://blog.csdn.net/jdsjlzx/article/details/76785239 有时在解析json数据中的字段key是动态可变的时候,由于Gson是使用静态注解的 ...
- Web中JS(Javascript)解析JSON数据的方法
js解析json数据,可以使用JSON.parse()方法来实现解析.JSON.parse()方法可以解析JSON字符串,转换为 JavaScript 对象 下面我们就结合简单的代码示例,给大家介绍j ...
- ASP.NET中使用JObject和JArray解析Json数据 (实用、赞)
原文出处:ASP.NET中使用JObject和JArray解析Json数据 - 谢友海 - 博客园 本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据.话不多说,下 ...
- ASP.NET中使用JObject和JArray解析Json数据
本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据.话不多说,下面我们直接来看一个示例. 数据样例(模拟接口返回的Json字符串),如下所示: {"cod ...
- 在php中怎么解析json数据,php解析json数据
在我们使用编程语言的时候,对于数据的格式会出现不能直接使用的情况,所以就会有解析的操作.在php中有专门解析json的函数,那就是json_decode().想要进一步的运用这个函数,我们还需要对它的 ...
最新文章
- 论文 | 图像和谐化公开数据集:让前景和背景更“般配”
- linux qt libusb,Ubuntu15下Qt+libusb开发
- retinaface精度
- html5播放器声音小,html5网页播放声音
- pytorch 构建神经网络模型总结
- Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
- c语言数组插入一个数字 移位,如何将一个数组的元素循环左移?
- CPU调度算法——FCFS算法/SJF算法/优先级调度算法/RR算法
- vCenter 6.0 vsca 安装遇到的一些小问题
- Python机器登陆新浪微博代码示例
- Atitit 提升开发效率几大策略 目录 1. 提升效率三原则	2 1.1. 更少的工作	2 1.2. 优化配置减少等待	2 1.3. 提升一次性处理能力	2 2. 方法提升	3 2.1. 分类优
- 15 款MacBook Pro扩容之旅
- idea 使用中文汉化包教程
- J2Cache缓存的使用
- uni-app自动定位当前位置
- VUE报错rowserslist: caniuse-lite is outdated. Please run the following command: `npx browser
- 海南大学计算机科学与技术知乎,海南大学计算机科学与技术怎么样
- 微信小程序---修改背景颜色和单个页面的背景颜色
- 实习僧网站字体反爬破解思路及步骤分享
- 微信网页程序开发,如何解决后退时重复登录的问题
热门文章
- java实现二进制转16进制
- flink source 同步_如何生成 Flink 作业的交互式火焰图?
- 给单片机焼写程序需要什么东西_单片机怎么烧写程序
- sscanf fscanf函数格式化输入遇到\n问题
- html5 css3 设计模式,html5+css3设计模式
- php获取页面a标签内容_AKCMS常用标签代码整理
- ue linux转dos格式,uestudio中如何把dos格式转为unix
- java继承总结_java继承总结(二)
- python dataframe取列名_python – 获取列名在DataFrame中按其值排序
- 计算机学报格式_如何快速写好一篇格式正确的参考文献?