使用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. Java 容器 LinkedHashMap源码分析1

    同 HashMap 一样,LinkedHashMap 也是对 Map 接口的一种基于链表和哈希表的实现.实际上, LinkedHashMap 是 HashMap 的子类,其扩展了 HashMap 增加 ...

  2. win7系统窗口背景颜色设置为护眼色的方法---打开的任意窗口显示为护眼色,程序眼必备

    win7系统窗口背景颜色设置为护眼色的方法 1. 打开"窗口颜色与外观"对话框 方法1:从控制面板开始 控制面板\外观和个性化\个性化\窗口颜色和外观 方法2:桌面上鼠标右键,个性 ...

  3. DIN-A4 doublesided year calendar

    % DIN-A4 doublesided year calendar % Author: Robert Krause % License : Creative Commons attribution ...

  4. Mongodb 与 SQL 语句对照表

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  5. asp.net 进行发送邮箱验证

    利用发送邮件验证码进行注册验证 需要引用using System.Net.Mail;命名空间 #region /// <summary> /// 发送邮件 /// </summary ...

  6. HTML/HTML5

    HTML/HTML5 一.文档加载顺序. 文档入下,数字编号为加载顺序. <html><!--1--> <head><!--2--> <link ...

  7. 浅析JS模块规范:AMD,CMD,CommonJS

    from:https://www.jianshu.com/p/09ffac7a3b2c 随着JS模块化编程的发展,处理模块之间的依赖关系成为了维护的关键.   模块化 AMD,CMD,CommonJS ...

  8. easyui-layout系列之表单一(2)

    表单在我们的开发过程非常的常见,easyUI给我们提供了非常方便快捷的表单开发工具,使用熟练可以大大的提高后台开发速度,非常有必要熟练掌握. 1.Textbox-文本框 扩展自$.fn.validat ...

  9. requirejs的插件介绍与制作

    本文由作者郑海波授权网易云社区发布. 前言 我这里就不介绍requirejs了, 简而言之: requirejs是支持AMD规范的模块加载器, 事实上它也是AMD的最直接推动者. 现在可供挑选的开源模 ...

  10. Android 导入 aar 库文件

    1. 在需要导入 aar 的 module 目录下创建一个名叫 "aars" 的目录,并把 aar 文件复制到这里. 2. 在项目的 build.gradle 文件里添加 allp ...