有一个带有参数化构造函数的简单类.我正在寻找一种方法,将其应用于JSON反序列化,并使用最少的注释.
如何让杰克逊在没有明确JsonProperty
的情况下,通过名字来识别id
场?
Class:个
public final class MyRec {
final String id;
public MyRec1(String id) {
this.id = id;
}
}
除非字段上存在批注@JsonProperty("id")
,否则反序列化失败.
Deserialization attempt:个
new ObjectMapper().readValue("""{"id":"abc"}""", MyRec.class);
Exception:个
Cannot construct instance of `sample.MyRec` (although at least one Creator exists):
cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"id":"abc"}"; line: 1, column: 2]
com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot construct instance of `sample.MyRec` (although at least one Creator exists):
cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"id":"abc"}"; line: 1, column: 2]
at app//com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)