日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

SpringBoot jackson返回值中含有null的解決辦法

作者:Lzfnemo2009 更新時間: 2022-05-11 編程語言

1.遇到的問題:后端查詢到的數據中含有null值,會展示到表格中,需求是將null替換為空

?

2.解決辦法:

添加這個配置類就可以

@Configuration
public class JacksonConfig {
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
                jsonGenerator.writeString("");
            }
        });
        return objectMapper;
    }

 

3.注意:

這個是在yml文件中添加的配置,含義是:返回的json數據中,忽略為null的字段。

 jackson:
    default-property-inclusion: non_null

用這個方法實現前端展示空白的結果如下:

?出現了undefined,是因為后端會忽略為null的字段,那么前端就會展示未定義。

原文鏈接:https://blog.csdn.net/Lzfnemo2009/article/details/123231361