简体   繁体   English

Json 与 POJO + Jackson 具有相同名称和不同类型的任何字段的模式

[英]Json schema with anyOf field with same name and different types to POJO + Jackson

If have a schema with component A that contains AnyOf section with two items.如果有一个包含组件 A 的模式,其中包含带有两个项目的 AnyOf 部分。 The difference between them is that in one case child component C is array and in another one C is object but they have the same name B. Is it possible to handle it with jackson if I have two java objects for that?它们之间的区别在于,在一种情况下,子组件 C 是数组,而在另一种情况下,子组件 C 是 object 但它们具有相同的名称 B。如果我有两个 java 对象,是否可以使用 jackson 处理它?

I'm thinking if I can use interface with some annotations and jackson will determine the correct object...我在想我是否可以使用带有一些注释的接口,jackson 将确定正确的 object...

"A": {
        "type": "object",
        "anyOf": [
          {
            "properties": {
              "B": {
                "type": "array",
                "items": {
                  "type": "object",
                  "$ref": "#/components/schemas/C"
                }
              }
            },
            "additionalProperties": false
          },
          {
            "properties": {
              
              "B": {
                "type": "object",
                "$ref": "#/components/schemas/C"
              }
            },
            "additionalProperties": false
          }
        ]
      }

Lets suppose I have this in java假设我在 java 中有这个

public class AAnyOf1 {

  @JsonProperty("B")
  private List<C> b = new ArrayList<>();

...

}

public class AAnyOf2 {

  @JsonProperty("B")
  private C b = null;

...

}

This is very popular pattern to send in response a JSON Object instead of JSON Array with one JSON Object .这是一种非常流行的模式,可以发送一个JSON Object而不是JSON Array和一个JSON Object So, instead of:所以,而不是:

{
  "b": [
    {
      "id": 1
    }
  ]
}

API response looks like: API响应如下所示:

{
  "b": {
      "id": 1
    }
}

Jackson already handle this use case. Jackson已经处理了这个用例。 You need to enable ACCEPT_SINGLE_VALUE_AS_ARRAY feature and you need only one version of POJO :您需要启用ACCEPT_SINGLE_VALUE_AS_ARRAY功能并且您只需要一个版本的POJO

class AAnyOf {

  @JsonProperty("B")
  private List<C> b;

...

}

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

相关问题 Json 架构与 POJO 的任何字段 - Json schema with anyOf fields to POJO Jackson 如何 map 一个 Pojo 字段到 2 个(json)字段(相同的内容,不同的名称) - Jackson how map one Pojo field to 2 (json) fields (same content, different name) Jackson objectMapper 将不同的 json 属性映射到同一个 pojo - Jackson objectMapper mapping different json properties to same pojo 在Jackson中反序列化同名但不同类型的属性? - Deserializing attributes of same name but different types in Jackson? 将 json 反序列化为 pojo,其中 json 字段具有不同的数据类型 - Deserializing json to pojo where json field has different data types JSON Jackson 将不同的键解析为同一字段 - JSON Jackson parse different keys into same field Jackson 在同一个 POJO 中映射不同的展开元素 - Jackson map different unwrapped elements in same POJO 在 Java 中,如何将 JSON 或 XML 字符串映射到同一个 POJO,但 XML 具有与 JSON 不同的字段/属性名称? - In Java, how can I map a string that is either JSON or XML to the same POJO, but with the XML having one different field/attribute name from the JSON? Java Jackson对具有相同名称但不同类类型的对象进行反序列化 - Java Jackson deserialize objects of the same name but different class types 在java pojo中将相同的json字段设置为不同的属性 - Same json field set to different attribute in java pojo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM