Java Jackson JSON 反序列化多参数构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39123030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:53:47  来源:igfitidea点击:

Hymanson JSON deserialization with multiple parameters constructor

javajsonHymansonHymanson-modules

提问by PedroTanaka

I've been using FasterXML/Hymanson-Databind in my project for a while now, and all was working great, until I've discovered this postand started to use this approach to desserialize objects without the @JsonProperty annotations.

我已经在我的项目中使用 FasterXML/Hymanson-Databind 有一段时间了,一切都很好,直到我发现了这篇文章并开始使用这种方法来反序列化没有 @JsonProperty 注释的对象。

The problem is that when I have a constructor which take multiple parameters and decorate this constructor with the @JsonCreator annotation Hymanson throw the following error:

问题是,当我有一个构造函数接受多个参数并用 @JsonCreator 注释装饰这个构造函数时,Hymanson 会抛出以下错误:

Exception in thread "main" com.fasterxml.Hymanson.databind.JsonMappingException: 
Argument #0 of constructor [constructor for com.eliti.model.Cruiser, annotations: {interface com.fasterxml.Hymanson.annotation.JsonCreator=@com.fasterxml.Hymanson.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]

I've created a little projectto illustrate the problem, the class I'm trying to desserialize is this one:

我创建了一个小项目来说明这个问题,我试图反序列化的类是这个:

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

}

And the code to desserialize is like this:

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

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

}

I already tried to remove the @JsonCreator, but if I do so, the throws the following exception:

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

Exception in thread "main" com.fasterxml.Hymanson.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]

I have tried to issue a "mvn clean install", but the problem persists.

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

Just to include some extra information, I've researched thoroughly about this problem (GitHub issues, Blog posts, StackOverflow Q&A). Here are some debbuging/investigation that I have been doing on my end:

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

Investigation 1

调查一

javap -von the generated bytecode give me this:

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

 MethodParameters:
      Name                           Flags
      name
      maxSpeed

When talking about the constructor, so I guess that the -parametersflag is really being set for javac compiler.

在谈论构造函数时,我猜实际上是为 javac 编译器设置了-parameters标志。

Investigation 2

调查二

If I create a constructor with a single parameter the object gets initialized, but I want/need to use the multiple parameter constructor.

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

Investigation 3

调查3

If I use the annotation @JsonProperty on each field it works as well, but for my original project it is too much overhead since I have a lot of fields in the constructor (and also it gets very hard to refactor code with annotations).

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

The question that remain is: How can I make Hymanson work with multiple parameter constructor without annotations?

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

回答by Nithish Thomas

You need to add the annotation @JsonPropertyspecifying the name of the json property that needs to be passed to the constructor when creating the object.

您需要添加注释,@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;
  }

}

EDIT

编辑

I just tested using the below code and it works for me

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

import java.io.IOException;

import com.fasterxml.Hymanson.annotation.JsonCreator.Mode;
import com.fasterxml.Hymanson.databind.ObjectMapper;
import com.fasterxml.Hymanson.databind.SerializationFeature;
import com.fasterxml.Hymanson.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);

    }

}

It produces the following output

它产生以下输出

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

Make sure you have the compilerArgs in the pom file.

确保在 pom 文件中有 compilerArgs。

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

回答by sendon1982

@JsonCreatoris not required after having @JsonProperty("xxx")in the parameter

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