package com.macro.mall.portal.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;


/**
 * Jackson相关配置
 * 配置json不返回null的字段
 * Created by macro on 2018/8/2.
 */
@Configuration
public class JacksonConfig {
    @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 不序列化,就是为null的字段不参加序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        // 字段保留，将null值转为""
//        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
//        {
//            @Override
//            public void serialize(Object o, JsonGenerator jsonGenerator,
//                                  SerializerProvider serializerProvider)
//                    throws IOException, JsonProcessingException
//            {
//                jsonGenerator.writeString("");
//            }
//        });
        return objectMapper;
    }
}

@@ -863,9 +863,9 @@
 %E8%B1%A1%E9%83%BD%E5%B0%86%E6%8C%89
-%E6%94%B9
+%E8%AF%A5
 %E8%A7%84%E5%88%99%E8%BF%9B%E8%A1%8C
@@ -983,31 +983,26 @@
  %E5%B1%9E%E6%80%A7%E4%B8%BA
- 
 %E7%A9%BA%EF%BC%88%22%22%EF%BC%89
- 
 %E6%88%96%E8%80%85%E4%B8%BA
- 
 NULL
- 
 %E9%83%BD%E4%B8%8D%E5%BA%8F%E5%88%97%E5%8C%96%EF%BC%8C
-%E5%88%99
 %E8%BF%94%E5%9B%9E%E7%9A%84j
@@ -1017,19 +1017,8 @@
 %E5%AD%97%E6%AE%B5%E7%9A%84%E3%80%82
-%E8%BF%99%E6%A0%B7%E5%AF%B9%E7%A7%BB%E5%8A%A8%E7%AB%AF%E4%BC%9A%E6%9B%B4%E7%9C%81%E6%B5%81%E9%87%8F
 %0A   
@@ -1053,27 +1053,12 @@
 NULL
- %E4%B8%8D%E5%BA%8F%E5%88%97%E5%8C%96,%E5%B0%B1%E6%98%AF%E4%B8%BAnull
 %E7%9A%84%E5%AD%97%E6%AE%B5%E4%B8%8D
-%E5%8F%82%E5%8A%A0
 %E5%BA%8F%E5%88%97%E5%8C%96%0A

Update JacksonConfig.java