0
点赞
收藏
分享

微信扫一扫

spring过滤值为null的JSON字段


问题说明

使用@ResponseBody注解的spring接口返回的JSON格式结果有时会返回包含值为null的字段,但是与前端联调可能并不希望包含这样的字段。
因此,需要过滤掉这类字段。

解决方案

​com.fasterxml.jackson.annotation.JsonInclude.Include​​指定是否返回值为null的字段。

如下配置所示:


1      

2

3

4

5

6

7

8

9

10

11

12

13

<mvc:annotation-driven>

<mvc:message-converters register-defaults="true">

<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>

举报

相关推荐

0 条评论