【问题标题】:Jackson JSON deserialization with multiple parameters constructor具有多参数构造函数的杰克逊 JSON 反序列化
【发布时间】:2016-12-31 13:44:21
【问题描述】:

我已经在我的项目中使用 FasterXML/Jackson-Databind 有一段时间了,一切都很好,直到我发现了这个 post 并开始使用这种方法来反序列化没有 @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]

我创建了一个little project 来说明问题,我要反序列化的类就是这个:

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;
  }

  public Integer getMaxSpeed() {
    return maxSpeed;
  }

  public void setMaxSpeed(Integer maxSpeed) {
    this.maxSpeed = maxSpeed;
  }

}

而反序列化的代码是这样的:

public class Test {
  public static void main(String[] args) throws IOException {
    Cruise cruise = new Cruise("asd", 100);
    cruise.setMaxSpeed(100);
    cruise.setCapacity(123);
    cruise.setInventor("afoaisf");

    ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
    mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

    String cruiseJson = mapper.writeValueAsString(cruise);

    System.out.println(cruiseJson);

    System.out.println(mapper.readValue(cruiseJson, Cruise.class));

}

我已经尝试删除 @JsonCreator,但如果我这样做,则会引发以下异常:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.eliti.model.Cruise: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: {
  "class" : "com.eliti.model.Cruise",
  "inventor" : "afoaisf",
  "type" : "MeansTransport",
  "capacity" : 123,
  "maxSpeed" : 100
}; line: 3, column: 3]

我尝试发出“mvn clean install”,但问题仍然存在。

为了包含一些额外的信息,我已经彻底研究了这个问题(GitHub 问题、博客文章、StackOverflow 问答)。以下是我一直在做的一些调试/调查:

调查一

javap -v 在生成的字节码上给我这个:

 MethodParameters:
      Name                           Flags
      name
      maxSpeed

在谈到构造函数时,我猜想 -parameters 标志确实是为 javac 编译器设置的。

调查2

如果我创建一个带有单个参数的构造函数,对象会被初始化,但我想要/需要使用多参数构造函数。

调查3

如果我在每个字段上使用 @JsonProperty 注释,它也可以工作,但是对于我的原始项目来说,开销太大,因为我在构造函数中有很多字段(而且使用注释重构代码也非常困难)。

剩下的问题是: 如何让 Jackson 在没有注释的情况下使用多参数构造函数?

【问题讨论】:

    标签: java json jackson jackson-modules


    【解决方案1】:

    如果您有嵌套的内部类,请将其设为静态

    public class A{
        private int property1;
        public static class B{
            private String property2;
        }
    }
    

    【讨论】:

      【解决方案2】:

      需要添加注解@JsonProperty指定创建对象时需要传递给构造函数的json属性的名称。

      public class Cruise extends WaterVehicle {
      
       private Integer maxSpeed;
      
        @JsonCreator
        public Cruise(@JsonProperty("name") String name, @JsonProperty("maxSpeed")Integer maxSpeed) {
          super(name);
          System.out.println("Cruise.Cruise");
          this.maxSpeed = maxSpeed;
        }
      
        public Integer getMaxSpeed() {
          return maxSpeed;
        }
      
        public void setMaxSpeed(Integer maxSpeed) {
          this.maxSpeed = maxSpeed;
        }
      
      }
      

      编辑

      我刚刚使用下面的代码进行了测试,它对我有用

      import java.io.IOException;
      
      import com.fasterxml.jackson.annotation.JsonCreator.Mode;
      import com.fasterxml.jackson.databind.ObjectMapper;
      import com.fasterxml.jackson.databind.SerializationFeature;
      import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
      
      class WaterVehicle {
      
          private String name;
          private int capacity;
          private String inventor;
          public WaterVehicle(String name) {
              this.name=name;
          }
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public int getCapacity() {
              return capacity;
          }
          public void setCapacity(int capacity) {
              this.capacity = capacity;
          }
          public String getInventor() {
              return inventor;
          }
          public void setInventor(String inventor) {
              this.inventor = inventor;
          }
      
      
      }
      
       class Cruise  extends WaterVehicle{
      
              private Integer maxSpeed;
      
              public Cruise(String name, Integer maxSpeed) {
                  super(name);
                  this.maxSpeed = maxSpeed;
              }
      
              public Integer getMaxSpeed() {
                  return maxSpeed;
              }
      
              public void setMaxSpeed(Integer maxSpeed) {
                  this.maxSpeed = maxSpeed;
              }
      
      
          }
      
      public class Test {
            public static void main(String[] args) throws IOException {
              Cruise cruise = new Cruise("asd", 100);
              cruise.setMaxSpeed(100);
              cruise.setCapacity(123);
              cruise.setInventor("afoaisf");
      
              ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
              mapper.registerModule(new ParameterNamesModule(Mode.PROPERTIES));
      
              String jsonString = mapper.writeValueAsString( cruise);
              System.out.println(jsonString);
      
              Cruise anotherCruise = mapper.readValue(jsonString, Cruise.class);
               System.out.println(anotherCruise );
               jsonString = mapper.writeValueAsString( anotherCruise );
               System.out.println(jsonString);
      
          }
      
      }
      

      它产生以下输出

      {
        "name" : "asd",
        "capacity" : 123,
        "inventor" : "afoaisf",
        "maxSpeed" : 100
      }
      Cruise@56f4468b
      {
        "name" : "asd",
        "capacity" : 123,
        "inventor" : "afoaisf",
        "maxSpeed" : 100
      }
      

      确保 pom 文件中有 compilerArgs。

      <compilerArgs>
           <arg>-parameters</arg>
      </compilerArgs>
      

      【讨论】:

      • 就是这样,我不想在每个构造函数参数中使用注解,因为在我的原始项目中,类中有很多参数。我想使用这样的东西:manosnikolaidis.wordpress.com.
      • 添加单参数字符串构造函数后,我的问题得到了解决。 @JsonProperty 不起作用。不知道是什么原因。
      【解决方案3】:

      @JsonCreator 在参数中有@JsonProperty("xxx") 后不再需要

      【讨论】:

        【解决方案4】:

        【讨论】:

        • 感谢您的意见,但此解决方案效果不佳。它似乎只适用于只有一个构造函数的类。当你有多个构造函数时,你必须提示 Jackson 使用哪一个来构造你的对象。
        猜你喜欢
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        • 2014-12-18
        • 1970-01-01
        • 2017-12-26
        • 1970-01-01
        相关资源
        最近更新 更多