简体   繁体   English

Java Jackson对具有相同名称但不同类类型的对象进行反序列化

[英]Java Jackson deserialize objects of the same name but different class types

I have POJOs that are used as the request and response object in a REST API like so (I know duplicate @JsonProperty isn't syntactically correct, see below): 我有像这样的REST API中用作请求和响应对象的POJO(我知道重复的@JsonProperty在语法上不正确,请参见下文):

public class Request {

    @JsonProperty("patient")
    PatientObjectA patientA;

    @JsonProperty("patient")
    PatientObjectB patientB;
}

public class PatientObjectA {
    @JsonProperty("identifier")
    Private Identifier identifier

    @JsonProperty("system")
    Private String system;

    @JsonProperty("value")
    Private String value;
}

public class PatientObjectA {
    @JsonProperty("identifier")
    Private List<Identifier> identifier

    @JsonProperty("system")
    Private String system;

    @JsonProperty("value")
    Private String value;
}

There are minor differences in cardinality in that I want to be able to consume ie the "Patient" object will sometimes be (PatientObjectA in Request class): 在基数方面存在细微的差异,因为我希望能够使用,即有时会使用“ Patient”对象(Request类中的PatientObjectA):

"patient": {
  "identifier": {
    "type": {
      "coding": {
        "system": "NA",
        "code": "Patient"
      },
      "text": "Patient"
    },
    "system": "Patient",
    "value": "000000000"
  }
}

or this case (note the differences in cardinality on the identifier object, where in this case identifier can have one or more items) (PatientBObject in Request class): 或这种情况(请注意,标识符对象的基数不同,在这种情况下,标识符可以包含一个或多个项目)(Request类中的PatientBObject):

    "patient": {
      "identifier": [{
         "type": {
          "coding": {
            "system": "NA",
            "code": "Patient"
          },
          "text": "Patient"
        },
        "system": "Patient",
        "value": "3018572032"
      }]
    }

I would like to achieve a functionality where requests are mapped to the correct objects. 我想实现将请求映射到正确对象的功能。 Is there a way (other than a custom deserializer) where I can map the requests to the appropriate object by type/cardinality? 有没有一种方法(自定义解串器除外)可以按类型/基数将请求映射到适当的对象? Any insight would be appreciated! 任何见识将不胜感激!

Jackson support this with the @JsonTypeInfo annotation. Jackson通过@JsonTypeInfo批注支持此功能。

I recommend specifying the type info in a property (a json field) and use the full class name (as opposed to a short name) to provide a better guarantee of uniqueness: 我建议在属性(json字段)中指定类型信息,并使用完整的类名(而不是简称)来提供更好的唯一性保证:

@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType") public class PatientObjectA { ..

Output A looks like: "patient": { "jsonType": "com.company.PatientAObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } } Output B looks like: "patient": { "jsonType": "com.company.PatientBObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } } 输出A看起来像: "patient": { "jsonType": "com.company.PatientAObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } }输出B看起来像: "patient": { "jsonType": "com.company.PatientBObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } }

Note: Also, check out @JsonRootName, as it will give you the ability to create a 'rooted' json object without having to have that wrapper object you have. 注意:另外,请检出@JsonRootName,因为它将使您能够创建“ rooted” json对象,而不必拥有该包装对象。

@JsonRootName("Patient") @JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType") public class PatientObjectA { ..

.. and .. ..和..

@JsonRootName("Patient") @JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType") public class PatientObjectB { ..

Related terms to assist with more research: 相关术语可帮助进行更多研究:

  • polymorphism in json json中的多态性
  • json equivalent of xml namespaces. 相当于xml名称空间的json。

Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object>

暂无
暂无

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

相关问题 使用 ObjectMapper 反序列化具有相同属性但 class 名称不同的多个对象 - Deserialize multiple objects with same attributes but different class name using ObjectMapper 在Jackson中反序列化同名但不同类型的属性? - Deserializing attributes of same name but different types in Jackson? Jackson 反序列化对象为同一个 - Jackson deserialize objects as the same one 将JSON类反序列化为其他名称的Java类 - Deserialize JSON class to Java class of different name 对不同类型的对象具有相同验证规则的 Java 设计类 - Java design class with same validation rules for different types of objects 使用 Jackson 和不同类型的 object 反序列化 JSON - Deserialize JSON using Jackson with different types of object 使用Jackson序列化/反序列化不同类型的地图 - Serialize/Deserialize maps with different types with Jackson Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object> Json 与 POJO + Jackson 具有相同名称和不同类型的任何字段的模式 - Json schema with anyOf field with same name and different types to POJO + Jackson 使用 Jackson 将列表和对象反序列化为相同的结构 - Deserialize Lists and Objects to the same Structure with Jackson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM