Spring Boot ObjectMapper 不返回 null 字段

Spring Boot About 453 words

需求

在使用ObjectMapper或正常Controller返回时,过滤掉值为null的字段。

代码

主要是objectMapper.setSerializationInclusion设置为JsonInclude.Include.NON_NULL

@Configuration
public class ObjectMapperConfig {

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

}
Views: 516 · Posted: 2024-04-05

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh