您好,登錄后才能下訂單哦!
在項目中有事需要對值為NULL的對象中Field不做序列化輸入配置方式如下:
[配置類型]:
源碼包中的枚舉類:
public static enum Include { ALWAYS, NON_NULL, NON_ABSENT, NON_EMPTY, NON_DEFAULT, USE_DEFAULTS; private Include() { } }
Include.Include.ALWAYS 默認
Include.NON_DEFAULT 屬性為默認值不序列化
Include.NON_EMPTY 屬性為 空(“”) 或者為 NULL 都不序列化
Include.NON_NULL 屬性為NULL 不序列化
方式一:全局配置,處理所有整個應用的實體對象
#對日期類型的轉換配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss # 配置 參數如下 always non_absent non_default non_empty non_null use_defaults spring.jackson.default-property-inclusion=non_null
方式二:在需要序列的話的實體類上加注解 ->[配置類型]所列
@JsonInclude(Include.NON_NULL)
方式三:配置類型
3.1自定義序列化實現類,可以作用在類上 自定義json序列化需要實現StdSerializer<T>或者JsonSerializer<T>
@JsonSerialize(using = ClientObjectSerialize.class) public class CreditBorrowerRepaymentRequestDto{ }
實現類:對字段類型轉換,以及對值為null字段的過濾
public class ClientObjectSerialize extends JsonSerializer<CreditBorrowerRepaymentRequestDto>{ @Override public void serialize(CreditBorrowerRepaymentRequestDto dto, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); try { Field[] fields = dto.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if(null == field.get(dto)){ continue; } jsonGenerator.writeFieldName(field.getName()); jsonGenerator.writeObject(field.get(dto)); } } catch (Exception e) { e.printStackTrace(); } jsonGenerator.writeEndObject(); } }
3.2自定義序列化實現類,可以作用在實體對象字段上,對NULL值的處理,或者轉換
@JsonSerialize(using = ClientStringSerialize.class) private String name; @JsonSerialize(using = ClientDtaeSerialize.class) private Date date; public class ClientStringSerialize extends JsonSerializer<String> { @Override public void serialize(String string, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { if(string == null){ jsonGenerator.writeString(string + "[NULL]"); }else{ jsonGenerator.writeString(string); } } } public class ClientDtaeSerialize extends JsonSerializer<Date> { @Override public void serialize(Date createDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createDate)); } }
實踐總結:
一當全局配置了NULL字段過濾的配置,但有的實體對象需要序列化出NULL的字段值,如何處理?
答:
1. 直接在對象上增加 @JsonInclude(JsonInclude.Include.ALWAYS) 類上的注解優先級比較高,會覆蓋全局的配置
2.用自定義的類序列化注解(同上)
二直接在字段上加自定義序列化類會覆蓋全局配置嗎?
答:不會,有默認的 public class NullSerializer extends StdSerializer<Object> 來處理,當值不為Null的時候才會執行自定義字段上的序列化注解實現類
補充知識:SpringBoot中Jackson返回null處理,字符串類型轉空串,數組集合轉[],對象轉{}
SpringBoot返回Json數據中null值處理,將字符串類型null值轉換為"",將集合數組類型null值轉換為[],將原始數據類型null值轉換為0,將布爾類型null值轉換為false,將實體對象null值轉換為{}。
1.自定義null值序列化處理器
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; /** * 自定義null值序列化處理器 */ public class CustomizeNullJsonSerializer { /** * 處理數組集合類型的null值 */ public static class NullArrayJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartArray(); jsonGenerator.writeEndArray(); } } /** * 處理字符串類型的null值 */ public static class NullStringJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } } /** * 處理數值類型的null值 */ public static class NullNumberJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeNumber(0); } } /** * 處理boolean類型的null值 */ public static class NullBooleanJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeBoolean(false); } } /** * 處理實體對象類型的null值 */ public static class NullObjectJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartObject(); jsonGenerator.writeEndObject(); } } }
2.序列化程序修改器
import com.fasterxml.jackson.databind.BeanDescription; import com.fasterxml.jackson.databind.SerializationConfig; import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; import java.util.Collection; import java.util.List; /** * <pre> * 此modifier主要做的事情為: * 1.當序列化類型為數組集合時,當值為null時,序列化成[] * 2.String類型值序列化為"" * * </pre> */ public class MyBeanSerializerModifier extends BeanSerializerModifier { @Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { // 循環所有的beanPropertyWriter for (int i = 0; i < beanProperties.size(); i++) { BeanPropertyWriter writer = beanProperties.get(i); // 判斷字段的類型,如果是數組或集合則注冊nullSerializer if (isArrayType(writer)) { // 給writer注冊一個自己的nullSerializer writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullArrayJsonSerializer()); } if (isStringType(writer)) { writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullStringJsonSerializer()); } } return beanProperties; } /** * 是否是數組 */ private boolean isArrayType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.isArray() || Collection.class.isAssignableFrom(clazz); } /** * 是否是String */ private boolean isStringType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz); } /** * 是否是數值類型 */ private boolean isNumberType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return Number.class.isAssignableFrom(clazz); } /** * 是否是boolean */ private boolean isBooleanType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.equals(Boolean.class); } }
3.配置Jackson實體
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; /** * 配置Jackson實體 */ @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); /** 為objectMapper注冊一個帶有SerializerModifier的Factory */ objectMapper.setSerializerFactory(objectMapper.getSerializerFactory() .withSerializerModifier(new MyBeanSerializerModifier())); SerializerProvider serializerProvider = objectMapper.getSerializerProvider(); serializerProvider.setNullValueSerializer(new CustomizeNullJsonSerializer .NullObjectJsonSerializer()); return objectMapper; } }
以上這篇springboot項目中jackson-序列化-處理 NULL教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。