小编Ped*_*aka的帖子

Jackson JSON使用多个参数构造函数进行反序列化

我已经在我的项目中使用了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)

java json jackson jackson-modules

21
推荐指数
3
解决办法
4万
查看次数

标签 统计

jackson ×1

jackson-modules ×1

java ×1

json ×1