@JsonInclude(Include.NON_NULL)全局配置

官方文檔 跳轉html

一、springMVC.xmljava

<!-- 默認的註解映射的支持 好比requestMapper之類的 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion"> 
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> 
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

  

二、spring boot 的配置web

配置文件加上:spring

spring.jackson.default-property-inclusion=non_null

  

或者:json

 1 @Configuration
 2 @EnableWebMvc
 3 @Slf4j
 4 public class WebMvcConfig extends WebMvcConfigurerAdapter {
 5    
 6     //@JsonInclude(Include.NON_NULL)全局配置
 7     @Override
 8     public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
 9         Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
10                 .serializationInclusion(JsonInclude.Include.NON_NULL);
11         converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
12     }
13 }

 

相關文章
相關標籤/搜索