Spring boot FastJson
介绍:FastJson 是ailibaba 的一款解析Json的开源框架
使用方式1
引入jar 包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
@SpringBootApplication
//启动类extends WebMvcConfigurerAdapter
public class App extends WebMvcConfigurerAdapter{
//重写configureMessageConverters
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//是否需要格式化
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
} public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
使用方式2
@SpringBootApplication
public class App {
//使用@Bean 依赖注入
@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);
} public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
FastJson 其他用法:
格式化:
@JSONField(format = "yyyy-MM-dd")
private Date createDate;
自定义名称:
@JSONField(name = "AGE")
private int age;
将 Java 对象转换换为 JSON 对象:
JSON.toJSONString()
创建JSON 对象:
public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 2; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("AGE", 10);
jsonObject.put("FULL NAME", "Doe " + i);
jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
jsonArray.add(jsonObject);
}
String jsonOutput = jsonArray.toJSONString();
}
JSON 对象需要转java对象:
public void whenJson_thanConvertToObjectCorrect() {
Person person = new Person(20, "John", "Doe", new Date());
String jsonObject = JSON.toJSONString(person);
Person newPerson = JSON.parseObject(jsonObject, Person.class)
}
Spring boot FastJson的更多相关文章
- Spring Boot fastJSON的使用
springBoot,默认使用的json解析框架是Jackson. 虽然jackson能够满足json的解析,如果想使用熟悉的alibaba的fastjon,我们只需要在pom文件中配置maven依赖 ...
- 在spring boot环境中使用fastjson + redis的高速缓存技术
因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...
- spring boot 之fastJson的使用(二)
昨天说了springboot的简单入门程序.今天进一步深入.今天说一下,fastJson的使用.做过springmvc的都知道fastjson.其实boot自带json可是本人用惯了fastjson, ...
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
- spring boot (二):使用fastJson解析json数据
如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...
- spring boot配置使用fastjson
一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...
- Spring Boot返回json数据及完美使用FastJson解析Json数据
Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...
- FastJson/spring boot: json输出方法二
1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...
- FastJson/spring boot: json输出
1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...
随机推荐
- js中值的基本类型与引用类型,以及对象引用,对象的浅拷贝与深拷贝
js有两种类型的值:栈:原始数据类型(undefinen,null,boolead,number,string)堆:引用数据类型(对象,函数和数组)两种类型的区别是:储存位置不同,原始数据类型直接存储 ...
- SpringBoot-@async异步执行方法
启动加上@EnableAsync ,需要执行异步方法上加入 @Async 在方法上加上@Async之后 底层使用多线程技术 演示代码: @RestController @Slf4j public c ...
- Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间
import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; ...
- JavaScript学习(八)
- 预见2019吴晓波年终秀演讲PPT整理
在2018年倒数的第二天12月30日晚上7点在广东珠海横琴拉开帷幕,吴晓波以一场“预见2019”的年终盛典,和大家一起回望即将告别的跌宕一年,细数过去的焦虑和改变,瞭望未来的激越和走向.下面我们一起来 ...
- MySQL中的数据类型以及完整性约束
数据类型 数据库mysql中也是分很多数据类型的,最常用的就是:数字类型.字符类型.日期类型.枚举与集合类型 一.数字类型: 默认都是有符号的,即正负号,若想无符号,在创建表时加unsigned.指定 ...
- APK反编译、重编译、签名、查看源码
1.反编译与重编译 工具:apktool 下载地址:https://ibotpeaches.github.io/Apktool/ 环境:Java (JRE 1.7) 安装步骤:参考官网(也可以不安装, ...
- 关于运行python脚本产生__pycache__
是什么? 用python编写好一个工程,在第一次运行后,总会发现工程根目录下生成了一个__pycache__文件夹,里面是和py文件同名的各种 *.pyc 或者 *.pyo 文件. python脚本编 ...
- 282A
#include <iostream> #include <string> using namespace std; int main() { int n, plus, sub ...
- python 类组合
场景:有一辆车违章需要通知到车主 定义两个类车辆类和通知类,车辆类和通知类并没有共同点,但是要通知车主这两个类就有了关联,这时候可以把这两个类组合在一起 #!/usr/bin/python3 # -* ...