最近我將我的專案從帶有 Jackson 2.9.7 的 Spring Boot 2.1.0 更新到帶有 Jackson 2.12.5 的 Spring Boot 2.5.5。從那時起,我看到了 JSON 序列化的差異。
為了區分情況(a)任何值,(b)顯式空值和(c)我使用java.util.Optional和NON_NULL注釋的無值。
public interface MyProjection {
@JsonInclude(Include.NON_NULL)
Optional<String> getMyAttribute();
}
這與 Spring Boot 2.1.0/Jackson 2.9.7 完美配合。
| 欄位值 | 渲染的 JSON |
|---|---|
Optional.of("something") |
{ "myAttribute": "something" } |
Optional.empty() |
{ "myAttribute": null } |
null |
{} |
截至目前(在我更新之后)null似乎被處理為Optional.empty(). 它使得{ "myAttribute": null }雖然場與注解@JsonInclude(NON_NULL)。使用 Spring Boot 2.5.5/Jackson 2.12.5,它呈現:
| 欄位值 | 渲染的 JSON |
|---|---|
Optional.of("something") |
{ "myAttribute": "something" } |
Optional.empty() |
{ "myAttribute": null } |
null |
{ "myAttribute": null } |
這僅適用于Optional<>,但它可以按預期使用基本型別,如String, Integer, Boolean, ...
我想恢復舊的行為。有誰知道如何使用@JsonInclude(NON_NULL)with Optional<>?我是否缺少(新)配置?
uj5u.com熱心網友回復:
我已經使用 @JsonSerialize(include = Inclusion.NON_NULL)來解決問題并且它起作用了。
uj5u.com熱心網友回復:
有三種方法可以忽略 1) 處的空欄位。欄位級別 2)。班級3)。全球范圍內
@JsonInclude(Include.NON_NULL) 私有字串用戶名;
@JsonInclude(Include.NON_NULL)
public class Book implements Comparable<Book> {
private String title;
private String author;
private int price;
public Book(String title, String author, int price) {
this.title = title;
this.author = author;
this.price = price;
}
}
// let's create a book with author as null
Book book = new Book(null, null, 42);
ObjectMapper mapper = new ObjectMapper();
// configure ObjectMapper to exclude null fields whiel serializing
mapper.setSerializationInclusion(Include.NON_NULL);
String json =mapper.writeValueAsString(cleanCode);
System.out.println(json);
uj5u.com熱心網友回復:
經過幾次搜索,我找到了感興趣的測驗用例。
以下是 jackson 專案中可選的測驗用例的摘錄:
public void testConfigAbsentsAsNullsTrue() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true));
OptionalData data = new OptionalData();
String value = mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).writeValueAsString(data);
assertEquals("{}", value);
}
public void testConfigAbsentsAsNullsFalse() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(false));
OptionalData data = new OptionalData();
String value = mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).writeValueAsString(data);
assertEquals("{\"myString\":null}", value);
}
class OptionalData {
public Optional<String> myString = Optional.empty();
}
還有一個注釋 configureAbsentsAsNulls。 https://github.com/FasterXML/jackson-datatype-jdk8/blob/master/src/main/java/com/fasterxml/jackson/datatype/jdk8/Jdk8Module.java
For compatibility with older versions
* of other "optional" values (like Guava optionals), it can be set to 'true'. The
* default is `false` for backwards compatibility.
public Jdk8Module configureAbsentsAsNulls(boolean state) {
_cfgHandleAbsentAsNull = state;
return this;
}
所以你只需要設定 configureAbsentsAsNulls(true) 來回滾你以前的狀態。
https://github.com/FasterXML/jackson-datatype-jdk8/blob/master/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestConfigureAbsentsAsNulls.java
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336931.html
上一篇:當串列分頁的大小輸入設定為大于15時,SpringDataPagedListHolder沒有回傳正確的PageSize,為什么?
下一篇:如何將@Scheduled()注釋CRON運算式正確外部化到我的application.properties檔案中?它無法決議占位符
