当前位置: 首页 > 知识库问答 >
问题:

使用多参数构造函数的杰克逊JSON反序列化

束向荣
2023-03-14

我在我的项目中使用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]

我已经创建了一个小项目来说明这个问题,我试图对这个类进行序列化:

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全新安装”,但问题仍然存在。

只是为了包含一些额外的信息,我已经彻底研究了这个问题(GitHub问题、博客帖子、StackOverflow Q

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

 MethodParameters:
      Name                           Flags
      name
      maxSpeed

当谈到构造函数时,所以我想-参数标志确实是为javac编译器设置的。

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

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

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

共有3个答案

苏高远
2023-03-14

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

尚鸿才
2023-03-14

简短的回答:使用 Java 8、javac 参数和杰克逊模块参数名称

长篇大论:为什么当构造函数用@JsonCreator注释时,其参数必须用@Json属性注释?

姜宏盛
2023-03-14

您需要添加注释@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 文件中有编译器Args。

<compilerArgs>
     <arg>-parameters</arg>
</compilerArgs>
 类似资料:
  • 在我的Spring开机项目中,我注意到了杰克逊奇怪的行为。我在网上搜索了一下,找到了该做什么,但还没找到原因。 用户收件人: 添加新用户效果很好。 TagDto: 尝试添加新标记以错误结束: com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造TagDto的实例(尽管至少存在一个创建者):无法从对象值反序列化(没有基于委托

  • 问题内容: 我正在尝试使用杰克逊对物体进行去电 我有这个例外: 我知道这正在发生,因为这是我的构造函数: 因此,我的构造函数接收到HttpResponse参数,但我没有传递它,但我不知道该怎么做。我不能用一个空的构造函数过度计费,因为我需要以这种方式接收HttpResponse对象。当我调用readValue()方法时,有什么方法可以传递此构造函数参数?或者在这种情况下最好的选择是什么?我感谢您的

  • 问题内容: 我正在使用Jackson示例代码对POJO进行反序列化: 这行抛出一个NoSuchMethodError: 我不明白 问题答案: 我猜您的Jackson JAR不同步。本类是JAR,和类是在。 确保它们都是相同的版本。

  • 问题内容: 我有一个Java类,我对从JSON反序列化感兴趣。我已经配置了一个特殊的MixIn类,以帮助我进行反序列化。只有和实例变量与适当的getter和setter方法相结合。看起来像这样: 在我的测试客户端中,我执行以下操作,但是在编译时它当然不起作用,因为与类型不匹配有关。 我知道我可以通过创建一个仅包含一个“响应”对象的方法来缓解此问题,但是随后我将不得不为我想返回的每种类型创建这些有点

  • 我正在调用一个返回JSON的endpoint,它看起来像这样(在Postman中): 此请求返回的Content-Type头是(与通常的 类来自外部库(编写这个endpoint的人)。无论如何,当我试图通过< code > rest template . exchange()调用这个endpoint时,Jackson都无法将这个JSON反序列化为一个有效的< code>Result类。我正在这样做

  • 有没有办法让Jackson序列化某个流对象(并在之后关闭)?这样地: 使现代化 澄清:我想流式传输内容,而不仅仅是将其序列化到单个String对象。