使用fastjson作为springboot的默认json解析, 原来使用的是jackson

1, 引入依赖

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>

注意: FastJsonHttpMessageConverter4支持4.2以上版本

   FastJsonHttpMessageConverter,支持4.2以下的版本

第一种方法:

(1)启动类继承extends WebMvcConfigurerAdapter

(2)覆盖方法configureMessageConverters

@SpringBootApplication
public class ApiCoreApp extends WebMvcConfigurerAdapter { @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);

     // 定义一个convert转换消息对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    
    // 添加fastjosn配置信息, 比如是否格式化返回的json数据
     FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
     // convert中添加配置
fastConverter.setFastJsonConfig(fastJsonConfig);
// 添加到convert中
converters.add(fastConverter);
}
}

第二种方法:

(1)在App.java启动类中,注入Bean : HttpMessageConverters

package com.iwhere;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @RestController
@SpringBootApplication
public class App {
    
    
   

 

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
  FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  FastJsonConfig fastJsonConfig = new FastJsonConfig();
  fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
  SerializerFeature.WriteMapNullValue,
  SerializerFeature.WriteNullListAsEmpty,
  SerializerFeature.WriteNullNumberAsZero,
  SerializerFeature.WriteNullBooleanAsFalse,
  SerializerFeature.WriteNullStringAsEmpty);
  fastConverter.setFastJsonConfig(fastJsonConfig);

  // 返回HttpMessageConvert对象
  HttpMessageConverter<?> converter = fastConverter;
  return new HttpMessageConverters(converter);
}


}

测试方法:

在实体类中使用@JSONField(Serialize=false) 字段不返回即是成功

在data字段上添加

@JSONFIled(format="yyyy-mm-dd HH:mm:ss")

既可以实现自动转化

本博客来源: http://412887952-qq-com.iteye.com/blog/2315202

springboot-4-整合fastjson的更多相关文章

  1. springboot整合fastJson遇到重定向问题

    通过网上教程使用springboot整合fastJson后遇到页面重定向问题(使用的springboot版本是2.0.2.RELEASE ,其他的版本可能不会出现以下问题),如下图: 我的项目结构如下 ...

  2. springboot下整合各种配置文件

    本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等  以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...

  3. springBoot 官方整合的redis 使用教程:(StringRedisTemplate 方式存储 Object类型value)

    前言:最近新项目准备用 redis 简单的缓存 一些查询信息,以便第二次查询效率高一点. 项目框架:springBoot.java.maven  说明:edis存储的数据类型,key一般都是Strin ...

  4. SpringBoot2.0整合fastjson的正确姿势

            SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将J ...

  5. SpringBoot 同时整合thymeleaf html、vue html和jsp

    问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...

  6. SpringBoot+AOP整合

    SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...

  7. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

  8. SpringBoot+Swagger整合API

    SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...

  9. springboot+maven整合spring security

    springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...

  10. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

随机推荐

  1. NC入门笔记

    简介: NC(全名NetCat),在网络界有"瑞士军刀"的美誉.它小巧而强悍,被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强大的网络调试和探测 ...

  2. hdu 5038 求出现次数最多的grade

    http://acm.hdu.edu.cn/showproblem.php?pid=5038 模拟水题 求出现次数最多的grade.如果有多个grade出现的次数一样多,且还有其他的grade,则把这 ...

  3. Vue2.5 Web App 项目搭建 (TypeScript版)

    参考了几位同行的Blogs和StackOverflow上的许多问答,在原来的ng1加TypeScript以及Webpack的经验基础上,搭建了该项目,核心文件如下,供需要的人参考. package.j ...

  4. Eclipse ADT 与VS 常用的快捷键 对比学习

    注:以下说的类型于VS,是指:VS+Resharper的快捷键,我是采用了Resharper作为VS的快捷键. 导航 Ctrl+1 快速修复 (类似于VS的alt+enter) Ctrl+D: 删除当 ...

  5. Linux 防火墙iptables命令详解

    [转:原文链接] iptables -Fiptables -Xiptables -F -t mangleiptables -t mangle -Xiptables -F -t natiptables ...

  6. 如何使用linq读取DataTable集合?AsQueryable() 和 AsEnumerable()区别?

    一.准备工作 引入linq和data 相关的using命名空间 DataTable dt=new DataTable();//dt的来源可以是多个地方,比如:数据库,Excel等等.我这里使用Exce ...

  7. .netcore部署centos

    前言:最近公司有个项目用 .netcore开发的项目,然后闲的没事就研究如果发布到Linux系统上 需要安装的插件以及支撑架构 1.dotnetSDK 2.jexus Jexus 是Linux平台上 ...

  8. NGUI图集字体

    UIFont里使用Symbols来指定字体时用Sprite前缀和名字自动分配的工具,前段时间工作需要时写的,具体用法有空时再写. using UnityEngine; using UnityEdito ...

  9. Docker容器的原理与实践 (下)

    欢迎访问网易云社区,了解更多网易技术产品运营经验. Docker原理分析 Docker架构 镜像原理 镜像是一个只读的容器模板,含有启动docker容器所需的文件系统结构及内容Docker以镜像和在镜 ...

  10. oh-my-zsh 安装及使用

    什么是 oh-my-zsh oh-my-zsh 是基于 zsh 的功能做了一个扩展,方便的插件管理.主题自定义,以及漂亮的自动完成效果. oh-my-zsh 安装及插件安装 查看系统中 zsh 及版本 ...