首页 > 技术文章 > SpringBoot 整合Jackson/FastJson/Gson序列化

ruhuanxingyun 2019-05-28 14:59 原文

一、Jackson

  简介:社区十分活跃,且更新速度快。

  1. SpringBoot JSON工具包默认是Jackson,只需要引入spring-boot-starter-web依赖包,自动引入相应依赖包: 

<dependency>

        <groupId>com.fasterxml.jackson.core</groupId>

        <artifactId>jackson-databind</artifactId> -->数据绑定依赖于下面两个包

        <version>2.8.7</version>

</dependency>

<dependency>

        <groupId>com.fasterxml.jackson.core</groupId>

        <artifactId>jackson-annotations</artifactId> -->注解包

        <version>2.8.0</version>

</dependency>

<dependency>

        <groupId>com.fasterxml.jackson.core</groupId>

        <artifactId>jackson-core</artifactId> -->核心包

        <version>2.8.7</version>

</dependency>

  2. Jackson两种配置方式

    A. application.properties文件

# 日期格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# 日期时区
spring.jackson.time-zone=GMT+8
# 返回值null不显示
spring.jackson.default-property-inclusion=non_null

    B. bean配置

package com.ruhuanxingyun.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfig {

@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 返回值过滤null或""值
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY).setSerializationInclusion(JsonInclude.Include.NON_NULL);

return objectMapper;
}

}

 

二、FastJson

  简介:阿里巴巴开发的,性能好,但是BUG多,有点“一快遮百丑”。

  1. pom.xml Maven依赖

<!-- Fastjson处理器 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.73</version>
</dependency>

  2. 配置类

package com.ruhuanxingyun.minio.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.nio.charset.StandardCharsets;

/**
 * @description: FastJson处理器 配置类
 * @author: ruphie
 * @date: Create in 2020/12/19 22:23
 * @company: ruhuanxingyun
 */
@Configuration
public class FastjsonConfig {

    @Bean
    public HttpMessageConverter httpMessageConverter() {
        // 定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 添加fastJson配置信息
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        fastJsonHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);

        return fastJsonHttpMessageConverter;
    }

}

   3. 时间格式化

    A. 全局配置(好像对LocalDateTime不生效)

//全局指定了日期格式
 fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

    B. @JSONField注解

 

三、Gson

  简介:Gson是谷歌开发的,功能很全面。 

 

可参考:SpringBoot Jackson配置方式

    SpringBoot 返回值XML

    Java JSON解析

推荐阅读