springmvc 框架默認使用 jackson 出來 json 返回,fastjson 默認是不序列化輸出 null 字段的,而 jackson 默認則是輸出 null 字段。html
<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>
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) public class Shop { //... }
jackson 2.7 之前版本, 參考: https://stackoverflow.com/questions/30042507/for-spring-boot-1-2-3-how-to-set-ignore-null-value-in-json-serialization
spring.jackson.serialization-inclusion=non_null
java
jackson 如今版本配置, 參考: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-the-jackson-objectmapper
spring.jackson.default-property-inclusion=non_null
spring