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> ...
随机推荐
- 敏捷开发之Scrum扫盲篇(转)
现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后于他人,我也开始学习Scrum.今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的语言来描述Scrum中 ...
- java开发中乱码的解决
总结一下,在JavaWeb中针对各种情况处理中文乱码的方法. 首先我们看下,一个请求响应的流程 浏览器------------------>Servlet容器---------------> ...
- React之生命周期
哈喽,这是我的第一篇博客,请大家多多关照~ 追根溯源:What's the lifeCycle? 生命周期函数指在某一时刻组件会自动调用执行的函数: React生命周期概览: 接下来我们就着生命周期的 ...
- 由于找不到 MSVCR100.dll,无法继续执行代码
由于找不到 MSVCR100.dll,无法继续执行代码.重新安装程序可能会解决此问题 360软件管家中找到 进行安装即可
- Redis入门到高可用(九)——有序集合zset
一.数据结构 集合与有序集合,列表与有序集合的对比 二.主要API zadd 将一个或多个 member 元素及其 score 值加入到有序集 key 当中. zrem 移除有序集 key 中的一个或 ...
- Java 基础 常用API (System类,Math类,Arrays, BigInteger,)
基本类型包装类 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类 ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 414. Third Maximum Number_Easy
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- Centos 中 vi 和vim 的区别
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面. vim的这些优势主要体现在以下几个方面:1.多级撤消我们知道在vi里,按 u只能撤消上次 ...
- SmartGit 过期破解 - 授权文件 Free Trial License to Non-Commercial
亲测可用~ Windows: %APPDATA%/syntevo/SmartGit/OS X: ~/Library/Preferences/SmartGit/Unix/Linux: ~/.sm ...