1.可以在类上面家伙是那个注解
@JsonInclude(JsonInclude.Include.NON_NULL)
2.可以在Application.yml文件上配置方法
(可以在application.properties或application.yml中添加以下配置:)
application.properties中配置:
spring.jackson.default-property-inclusion=non_null
application.yml中配置
spring:
jackson:
default-property-inclusion: non_null
3.全局配置
也可以全局配置
package com.saferycom.nonull;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author LH
*/
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}
}