官方文档 跳转
非全局配置:
![]()
全局配置:
1、springMVC.xml
<!-- 默认的注解映射的支持 比如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>
2、spring boot 的配置
配置文件加上:
spring.jackson.default-property-inclusion=non_null
or 
如果配置不生效,则检查 @EnableWebMvc注释掉

或者:
@Configuration
@EnableWebMvc
@Slf4j
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//@JsonInclude(Include.NON_NULL)全局配置
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.serializationInclusion(JsonInclude.Include.NON_NULL);
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
}
















