SpringMVC - Json 不返回 null 字段 [简记]

继续后端服务系列:

spring mvc 使用 jackson 包处理,默认返回 null 字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"measures": [
{
"metric": null,
"periods": [
{
"index": 2,
"value": "0.0"
}
],
"component": null
}
]
}

省去这些 null 字段返回:

spring xml 配置:

1
2
3
4
5
6
7
8
9
<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>

效果:

1
2
3
4
5
6
7
8
9
10
11
12
{
"measures": [
{
"periods": [
{
"index": 2,
"value": "0.0"
}
]
}
]
}