介绍: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的更多相关文章

  1. Spring Boot fastJSON的使用

    springBoot,默认使用的json解析框架是Jackson. 虽然jackson能够满足json的解析,如果想使用熟悉的alibaba的fastjon,我们只需要在pom文件中配置maven依赖 ...

  2. 在spring boot环境中使用fastjson + redis的高速缓存技术

    因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...

  3. spring boot 之fastJson的使用(二)

    昨天说了springboot的简单入门程序.今天进一步深入.今天说一下,fastJson的使用.做过springmvc的都知道fastjson.其实boot自带json可是本人用惯了fastjson, ...

  4. spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...

  5. spring boot (二):使用fastJson解析json数据

    如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...

  6. spring boot配置使用fastjson

    一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...

  7. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  8. FastJson/spring boot: json输出方法二

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

  9. FastJson/spring boot: json输出

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

随机推荐

  1. 学习UDP

    参考博客:https://www.cnblogs.com/skyfsm/p/6287787.html 简单编写了 udp的小程序,udp_client 端通过终端输入字符,然后发送给udp_serve ...

  2. 异步fifo的Verilog实现

     一.分析 由于是异步FIFO的设计,读写时钟不一样,在产生读空信号和写满信号时,会涉及到跨时钟域的问题,如何解决? 跨时钟域的问题:由于读指针是属于读时钟域的,写指针是属于写时钟域的,而异步FIFO ...

  3. IAM:亚马逊访问权限控制

    IAM的策略.用户->服务器(仓库.业务体) IAM:亚马逊访问权限控制(AWS Identity and Access Management )IAM使您能够安全地控制用户对 AWS 服务和资 ...

  4. WebSocket 学习教程(一):理论

    一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通 ...

  5. 报错解决——SSL: CERTIFICATE_VERIFY_FAILED

    SSL: CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL ...

  6. 香港低价linux虚拟主机,

    https://www.sugarhosts.com/zh-cn/hosting/shared-web-hosting Shared Baby 36 个月 ¥ 26.99 19 99 · / 月 续费 ...

  7. vue/cli 3.0 font-size随屏幕大小变化而变化 rem设置

    在安装cube-ui框架时 安装成功后在[E:\WWW\xxx\node_modules\vue-cli-plugin-cube-ui\generator\rem\index.js]修改remUnit ...

  8. 调用另一个文件的python代码【转载】

    转自:https://blog.csdn.net/u010412719/article/details/47089883 例如我们有a.py和b.py两个文件,当我们需要在b.py文件中应用a.py中 ...

  9. 【LeetCode每天一题】3Sum(三数之和)

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  10. 使用Pyinstaller打包python py文件注意事项

    首先需要将cmd或者PowerShell目录cd 跳转到py脚本所在目录,然后直接执行 Pyinstaller -F即可完成程序的打包,其中我们关心它有哪些常见参数: 输入参数的含义 -F 表示生成单 ...