美文网首页SpringBoot极简教程 · Spring Boot
Springboot jackson配置转换方法重写

Springboot jackson配置转换方法重写

作者: zhangweisep | 来源:发表于2020-03-17 15:03 被阅读0次
package com.njnewmnet.house.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;

/**
 * Springboot jackson配置转换方法重写
 * Include.Include.ALWAYS:默认
 * Include.NON_DEFAULT:属性为默认值不序列化
 * Include.NON_EMPTY:属性为 空("") 或者为 NULL 都不序列化
 * Include.NON_NULL:属性为NULL 不序列化
 * @pathName:JacksonConfig
 * @author:ZhangWei
 */
@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper;
    }

}

相关文章

网友评论

    本文标题:Springboot jackson配置转换方法重写

    本文链接:https://www.haomeiwen.com/subject/hgetyhtx.html