简体   繁体   English

JSON Jackson从我期望的内容映射到其他构造函数

[英]JSON Jackson maps to other constructor from what I was expecting

I have this JSON: 我有这个JSON:

{ 
   "label":{    
            "label":"1D812", 
            "version":"01"
           },    
   "productionDate":"140415", 
   "boxNumber":"003",
   "quantity":11000, 
   "order":"0000", 
   "documentId":"DOC-HHS",
   "location":1
}

and I run these commands to create a Box object 我运行这些命令来创建Box对象

ObjectMapper mapper = new ObjectMapper();
Box box = mapper.readValue(myJSON, Box.class);

In Box class there is the following constructor: Box类中,有以下构造函数:

public Box(Label label, String productionDate, String boxNumber, int quantity, String order,  String documentId,int location ) {
   this.label = label;
   this.quantity = quantity;
   this.order = order;
   this.boxNumber = boxNumber;
   this.location = location;
   this.documentId = documentId;
   this.productionDate = productionDate;
}

And in Label class I have these constructors (in that order, if it matters) Label类中,我有这些构造函数(按顺序排列,如果有关系的话)

public Label() {
}    

public Label(String label) {
        this.label = label;
}

public Label(String label, String version) {
    this.label = label;
    this.version = version;
}

public Label(String label, String version, SupplierL supplier) {
    this.label = label;
    this.version = version;
    this.supplier = supplier;
    this.labelWithVersion = label + "-" + version;
}

When I System.out.println(box.toString()); 当我System.out.println(box.toString()); I see that: 我看到:

Box{label=Label{label=1D812, version=01, supplier=null}, etc...}

I am wondering, why it used the Label constructor with the 3 arguments and not that with the 2? 我想知道为什么将Label构造函数与3个参数一起使用,而不将其与2个参数一起使用?

I don't think it calls that 3-arg constructor at all. 我认为它根本没有调用3-arg构造函数。 How would it know what's what? 它怎么会知道是什么? Instead it calls the no-args constructor and set the field values. 而是调用no-args构造函数并设置字段值。

If you want it to call a particular constructor, use the @JsonCreator annotation. 如果希望它调用特定的构造函数,请使用@JsonCreator批注。 Note that you can only set it on one. 请注意,您只能将其设置为一个。

See this answer for details: 有关详细信息,请参见以下答案:

How to deserialize a class with overloaded constructors using JsonCreator 如何使用JsonCreator使用重载的构造函数反序列化类

There's no Supplier in your JSON object. JSON对象中没有供应商。 So it correcly used 2 argument constructor: 因此它正确地使用了2个参数构造函数:

"label":{    
        "label":"1D812", 
        "version":"01"
        }    

And that's exactly what you see in the output: 这就是您在输出中看到的:

label=Label{label=1D812, version=01, supplier=null}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Jackson从类中生成json模式,我需要对给定的输出做什么? - using Jackson to produce a json schema from a class, what do I need to do to the given output? FANNJ(Java中的FANN)构造函数期望的文件路径是什么? - What is the filepath that FANNJ (FANN in Java) constructor is expecting? 解决Json Jackson和Lombok构造函数要求 - Getting around Json jackson and lombok constructor requirements 用Jackson反序列化JSON - 为什么JsonMappingException“没有合适的构造函数”? - Deserializing JSON with Jackson - Why JsonMappingException “No suitable constructor”? 问题 Jackson JSON 和 Lombok 构造函数的反序列化 - Problem Jackson deserialization of JSON and Lombok constructor Jackson JSON 反序列化多参数构造函数 - Jackson JSON deserialization with multiple parameters constructor Jackson ObjectMapper无法反序列化POJO,引发异常:没有找到适合类型[…]的合适构造函数:无法从JSON对象实例化 - Jackson ObjectMapper cannot deserialize POJO, throws an exception: No suitable constructor found for type […]: can not instantiate from JSON object 需要使用Jackson来反序列化此JSON-[{},{},{}]-用于JSON数组的Jackson注释是什么? - Need to deserialize this JSON with Jackson - [{}, {}, {}] - What Jackson annotation to use for JSON array? 使用 Jackson 将 Json 反序列化为其他类层次结构 - Json deserialization into other class hierarchy using Jackson 如何从Jackson @JsonCreator构造函数访问Spring Boot环境变量 - How can I access a Spring Boot environment variable from a Jackson @JsonCreator constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM