@JsonInclude(value = JsonInclude.Include.NON_NULL)
时间: 2024-05-28 21:13:30 浏览: 18
@JsonInclude(value = JsonInclude.Include.NON_NULL) 是 Jackson 库提供的注解,用于指示在序列化 Java 对象为 JSON 格式的过程中,仅包含非空的属性值。如果一个属性的值为 null,那么在序列化过程中该属性将被忽略掉,不会被包含在最终生成的 JSON 中。这个注解可以用在类级别或属性级别上。在类级别上使用 @JsonInclude 注解时,它将适用于整个类的所有属性。在属性级别上使用 @JsonInclude 注解时,它将仅应用于该属性。
相关问题
package com.design.common.utils; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import java.io.Serializable; @JsonInclude(value = Include.NON_NULL) public class ReturnData implements Serializable { private Boolean success; private Object data; private String errorCode; private String errorMessage; // 0 silent; 1 message.warn; 2 message.error; 4.notification; 9 page private Integer showType; private String traceId; private String host; public ReturnData() { } public ReturnData(Boolean success) { this.success = success; } public static ReturnData success() { return new ReturnData(true); } public static ReturnData error() { return new ReturnData(false); } public Boolean getSuccess() { return success; } public ReturnData setSuccess(Boolean success) { this.success = success; return this; } public Object getData() { return data; } public ReturnData data(Object data) { this.data = data; return this; } public String getErrorCode() { return errorCode; } public ReturnData errorCode(String errorCode) { this.errorCode = errorCode; return this; } public String getErrorMessage() { return errorMessage; } public ReturnData errorMessage(String errorMessage) { this.errorMessage = errorMessage; return this; } public Integer getShowType() { return showType; } public ReturnData showType(Integer showType) { this.showType = showType; return this; } public String getTraceId() { return traceId; } public ReturnData traceId(String traceId) { this.traceId = traceId; return this; } public String getHost() { return host; } public ReturnData host(String host) { this.host = host; return this; } }
这段代码是一个示例的 ReturnData 类的实现,它包含了以下属性和方法:
- 属性:
- success: 表示操作是否成功的布尔值。
- data: 表示返回的数据对象。
- errorCode: 表示错误代码。
- errorMessage: 表示错误消息。
- showType: 表示错误展示类型的整数值。
- traceId: 表示追踪ID。
- host: 表示主机名。
- 构造函数:
- ReturnData(): 默认构造函数。
- ReturnData(Boolean success): 接受一个布尔参数的构造函数。
- 静态方法:
- success(): 返回一个成功的 ReturnData 实例。
- error(): 返回一个失败的 ReturnData 实例。
- getter 和 setter 方法:用于访问和设置属性值。
这个类使用了 Jackson 库的注解,用于指定在序列化时包含非空字段。它还实现了 Serializable 接口,以支持对象的序列化操作。
请注意,这只是一个示例代码,你可以根据自己的需求进行修改和扩展。
@ExcelProperty 映射integer字段为null
你可以在对应的 Integer 类型字段上使用注解 @JsonInclude(JsonInclude.Include.NON_NULL),这样在序列化该对象时,如果该字段的值为 null,则不会被序列化到 JSON 字符串中。同时,你也可以在该字段上使用注解 @JsonSerialize(using = NullToZeroSerializer.class),自定义一个序列化器,将 null 值序列化为 0。以下是示例代码:
```java
public class MyEntity {
@JsonInclude(JsonInclude.Include.NON_NULL)
private Integer myIntegerField;
// getter and setter
}
public class NullToZeroSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNumber(0);
} else {
gen.writeNumber(value);
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)