SPRINGBOOT返回數據NULL參數設爲空字符串或空數組 SPRINGBOOT返回數據NULL參數設爲空字符串或空數組

https://www.cnblogs.com/jthr/p/15825435.html

 

SPRINGBOOT返回數據NULL參數設爲空字符串或空數組

複製代碼
package com.ruoyi.framework.config.ResponseVoConfig.WebConfig;

/**
 * @Classname MyJsonMapper
 * @Description TODO
 * @Date 2022/1/20 0020 上午 10:39
 * @Created by jcc
 */
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/**
 * jackson 定製
 *
 * @author lqq
 */
public class MyJsonMapper extends ObjectMapper {
    public MyJsonMapper() {
        super();
        //收到未知屬性時不報異常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
        //Long類型轉爲String類型
        SimpleModule simpleModule = new SimpleModule();
        //simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        //simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        this.registerModule(simpleModule);
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); //設置日期格式
        //處理空指針時設置的值
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                String fieldName = gen.getOutputContext().getCurrentName();
                try {
                    //反射獲取字段類型
                    Field field = gen.getCurrentValue().getClass().getDeclaredField(fieldName);
                    if (Objects.equals(field.getType(), String.class)) {
                        //字符串型空值""
                        gen.writeString("");
                        return;
                    } else if (Objects.equals(field.getType(), List.class)) {
                        //列表型空值返回[]
                        gen.writeStartArray();
                        gen.writeEndArray();
                        return;
                    } else if (Objects.equals(field.getType(), Map.class)) {
                        //map型空值返回{}
                        gen.writeStartObject();
                        gen.writeEndObject();
                        return;
                    }
                } catch (NoSuchFieldException e) {
                }
                //默認返回""
                gen.writeString("");
            }
        });
    }
}
複製代碼

 

複製代碼
package com.ruoyi.framework.config.ResponseVoConfig.WebConfig;

/**
 * @Classname WebConfig
 * @Description TODO
 * @Date 2022/1/20 0020 上午 10:39
 * @Created by jcc
 */
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * web 相關配置
 *
 * @author lqq
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //在json轉換之前先進行string轉換
        converters.add(new StringHttpMessageConverter());
        //添加json轉換
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(new MyJsonMapper());
        converters.add(jackson2HttpMessageConverter);
        //5、追加默認轉換器
        super.addDefaultHttpMessageConverters(converters);
    }
}
複製代碼

 

 
分類: springboot
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章