問題背景
在Spring Boot中使用spring-boot-starter-json
(通常是通過jackson
實現的)時,如果你希望在序列化對象時,如果某個屬性為空,則不顯示該屬性,你可以使用@JsonInclude
注解來實現這一點。
pom.xml
<!-- springboot-json by zhengkai.blog.csdn.net -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId>
</dependency>
@JsonInclude
注解
@JsonInclude
注解可以控制序列化過程中包含哪些屬性。它有幾個參數,其中JsonInclude.Include
枚舉定義了不同的行為:
ALWAYS
:總是包括屬性,即使值為null或空。NON_NULL
:僅當屬性值不為null時才包括。NON_ABSENT
:對于Java Optional,僅當值為非空(即Optional.isPresent()為true)時才包括。NON_EMPTY
:對于集合或數組,僅當它們不為空時才包括。NON_DEFAULT
:僅當屬性值不等于其類型的默認值時才包括。
代碼實戰
package com.softdev.system.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;import java.io.Serializable;@JsonInclude(value = JsonInclude.Include.NON_NULL)
@Data
public class SysConfig implements Serializable {private static final long serialVersionUID = 1L;/*** configId*/@TableId(type = IdType.AUTO)Integer id;String paramKey;String paramValue;Integer status;String remark;
}
可以看到數據庫的Remark字段是空的
由于加了注釋,所以他并不會顯示出來?
移除@JsonInclude(value = JsonInclude.Include.NON_NULL)注釋
局部配置
如果你希望屬性為空時不顯示,可以在你的類或屬性上使用@JsonInclude(JsonInclude.Include.NON_NULL)
注解。例如:
import com.fasterxml.jackson.annotation.JsonInclude;
//by zhengkai.blog.csdn.net
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyBean {private String property1;private String property2;// getters and setters
}
在上面的例子中,如果property1
或property2
的值為null,它們將不會被包含在JSON序列化的結果中。
請注意,@JsonInclude
注解可以應用于類級別或屬性級別。如果應用于類級別,它將影響該類的所有屬性。如果應用于屬性級別,它將僅影響該特定屬性。
全局配置
這個配置可以通過application.properties
或application.yml
文件進行全局設置,例如:
# application.properties
spring.jackson.default-property-inclusion=non_null
或者
# application.yml
spring:jackson:default-property-inclusion: non_null
這樣配置后,所有的類和屬性都會遵循這個規則,除非它們被單獨覆蓋。
Customize the Jackson ObjectMapper
Spring MVC (client and server side) uses?HttpMessageConverters
?to negotiate content conversion in an HTTP exchange. If Jackson is on the classpath, you already get the default converter(s) provided by?Jackson2ObjectMapperBuilder
, an instance of which is auto-configured for you.
The?ObjectMapper
?(or?XmlMapper
?for Jackson XML converter) instance (created by default) has the following customized properties:
-
MapperFeature.DEFAULT_VIEW_INCLUSION
?is disabled -
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
?is disabled -
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
?is disabled -
SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS
?is disabled
Spring Boot also has some features to make it easier to customize this behavior.
You can configure the?ObjectMapper
?and?XmlMapper
?instances by using the environment. Jackson provides an extensive suite of on/off features that can be used to configure various aspects of its processing. These features are described in several enums (in Jackson) that map onto properties in the environment:
Enum | Property | Values |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example, to enable pretty print, set?spring.jackson.serialization.indent_output=true
. Note that, thanks to the use of?relaxed binding, the case of?indent_output
?does not have to match the case of the corresponding enum constant, which is?INDENT_OUTPUT
.
This environment-based configuration is applied to the auto-configured?Jackson2ObjectMapperBuilder
?bean and applies to any mappers created by using the builder, including the auto-configured?ObjectMapper
?bean.
The context’s?Jackson2ObjectMapperBuilder
?can be customized by one or more?Jackson2ObjectMapperBuilderCustomizer
?beans. Such customizer beans can be ordered (Boot’s own customizer has an order of 0), letting additional customization be applied both before and after Boot’s customization.
Any beans of type?com.fasterxml.jackson.databind.Module
?are automatically registered with the auto-configured?Jackson2ObjectMapperBuilder
?and are applied to any?ObjectMapper
?instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.
If you want to replace the default?ObjectMapper
?completely, either define a?@Bean
?of that type and mark it as?@Primary
?or, if you prefer the builder-based approach, define a?Jackson2ObjectMapperBuilder
?@Bean
. Note that, in either case, doing so disables all auto-configuration of the?ObjectMapper
.
If you provide any?@Beans
?of type?MappingJackson2HttpMessageConverter
, they replace the default value in the MVC configuration. Also, a convenience bean of type?HttpMessageConverters
?is provided (and is always available if you use the default MVC configuration). It has some useful methods to access the default and user-enhanced message converters.