我已经在我的项目中使用了FasterXML/Jackson-Databind一段时间,并且一切都很好,直到我发现这篇文章并开始使用这种方法来反序列化没有@JsonProperty注释的对象.
问题是,当我有一个构造函数接受多个参数并使用@JsonCreator注释装饰这个构造函数时,Jackson会抛出以下错误:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException:
Argument #0 of constructor [constructor for com.eliti.model.Cruiser, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
at [Source: {
"class" : "com.eliti.model.Cruiser",
"inventor" : "afoaisf",
"type" : "MeansTransport",
"capacity" : 123,
"maxSpeed" : 100
}; line: 1, column: 1]
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个小项目来说明问题,我试图反序列化的类是这个:
public class Cruise extends WaterVehicle {
private Integer maxSpeed;
@JsonCreator
public Cruise(String name, Integer maxSpeed) {
super(name);
System.out.println("Cruise.Cruise");
this.maxSpeed = maxSpeed; …Run Code Online (Sandbox Code Playgroud)