Springboot2.3.1搭建框架 Controller返回对象自动过滤null属性字段

请您多多留言指教

框架环境:

springboot2.3.1+mybatisplus3.3.2+jackson2.11.0 全局配置自动过滤null属性字段

1、application.yml 全局配置 jackson

jackson:
  # 全局设置@JsonFormat的格式pattern
  date-format: yyyy-MM-dd HH:mm:ss
  # 当地时区
  locale: zh
  # 设置全局时区
  time-zone: GMT+8
  serialization:
    #格式化输出
    indent_output: true
    #忽略无法转换的对象
    fail_on_empty_beans: false
  deserialization:
    #允许对象忽略json中不存在的属性
    fail_on_unknown_properties: false
  parser:
    #允许出现特殊字符和转义符
    allow_unquoted_control_chars: true
    #允许出现单引号
    allow_single_quotes: true
  #如果加该注解的字段为null,那么就不序列化这个字段了
  default-property-inclusion: NON_EMPTY

2、类的配置jackson.config

package com.ayiol.business.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
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;
import java.io.IOException;

/**
 * Jackson config
 * author LJG
 * date 2020/6/26 20:46
 */
@Configuration
public class JacksonConfig {
    /**
     * 重新注入ObjectMapper
     * 注:使用此方式ObjectMapper,application中Jackson自动失效
     *
     * @param builder
     * @return
     */
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        // 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
        // Include.Include.ALWAYS 默认
        // Include.NON_DEFAULT 属性为默认值不序列化
        // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json无此属性字段
        // Include.NON_NULL 属性为NULL 不序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        // 反序列化时 忽略匹配不到的属性字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 允许出现单引号
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        // 字段保留,将null值转为""
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator,
                                  SerializerProvider serializerProvider)
                    throws IOException {
                jsonGenerator.writeString("");
            }
        });
        return objectMapper;
    }
}

注:红色标记为重点,这种配置按需求使用(本人未使用:因需要返回相关为null的属性字段)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值