Spring-Boot 不返回字段为null值
方法一:在application.yml配置文件中
1 | spring: |
2 | jackson: |
3 | default-property-inclusion: non_null |
方法二:单DTO空值过滤,DTO上添加如下注解
1 | (JsonInclude.Include.NON_EMPTY) |
2 | (JsonInclude.Include.NON_NULL) |
方法三:自定义消息转换器
1 | |
2 | public class WebMvcConfig extends WebMvcConfigurerAdapter{ |
3 | /** |
4 | * 利用fastjson替换掉jackson |
5 | * @param converters |
6 | */ |
7 | |
8 | public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { |
9 | super.configureMessageConverters(converters); |
10 | FastJsonConfig fastJsonConfig = new FastJsonConfig(); |
11 | fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); |
12 | } |
13 | } |
方法四: 在xml中配置
1 | <!--开启注解支持,并配置处理器映射器和适配器 --> |
2 | <mvc:annotation-driven conversion-service="converterService_self"> |
3 | <!--jackson处理过的json null值过滤--> |
4 | <mvc:message-converters> |
5 | <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> |
6 | <property name="objectMapper"> |
7 | <bean class="com.fasterxml.jackson.databind.ObjectMapper"> |
8 | <property name="serializationInclusion"> |
9 | <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> |
10 | </property> |
11 | </bean> |
12 | </property> |
13 | </bean> |
14 | </mvc:message-converters> |
15 | </mvc:annotation-driven> |