SpringBoot项目请求返回json空字段过滤
接口返回的json中有的字段可能是为空的,我们不希望他为空的还返回,如下例子:
解决方案:只需要加一个配置类就行:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.nio.charset.StandardCharsets;
import java.util.List;/*** @author 咕噜科* ClassName: JsonRespConfiguration* date: 2023-08-20 19:28* Description: 用于配置 JSON 数据的序列化和反序列化。* version 1.0*/
@Configuration
public class JsonRespConfiguration implements WebMvcConfigurer {@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(mappingJackson2HttpMessageConverter());}@Beanpublic MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();ObjectMapper objectMapper = new ObjectMapper();//允许有未知字段映射不上objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);//过滤response的空值// Include.Include.ALWAYS 默认// Include.NON_DEFAULT 属性为默认值不序列化// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。// Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);converter.setDefaultCharset(StandardCharsets.UTF_8);converter.setObjectMapper(objectMapper);return converter;}
}
重新启动后,再看结果,null值的字段已经没返回了: