【问题标题】:Generate Multiple POJO's from single json schema file从单个 json 模式文件生成多个 POJO
【发布时间】:2015-01-19 06:53:10
【问题描述】:

我正在使用 jsonschema2pojo 来生成 POJO。它工作正常。 但我想从单个 json 模式文件生成多个 POJO。 这是否可以使用 jsonschema2pojo 插件。

生成多个 POJO 的一种方法是提供多个 json 模式文件,但我不想这样做。我只想提供一个 json 模式文件作为输入。

我提供以下 json 模式作为输入:

{
  "Address": {
    "description": "AnAddressfollowingtheconventionofhttp: //microformats.org/wiki/hcard",
    "type": "object",
    "properties": {
        "post-office-box": {
            "type": "string"
        },
        "post-office-address": {
            "type": "string"
        }
    },
    "additionalProperties": false
  },
  "AddressDetails": {
    "description": "AnAddressDetailsfollowingtheconventionofhttp: //microformats.org/wiki/hcard",
    "type": "object",
    "properties": {
        "post-office-box": {
            "type": "string"
        },
        "post-office-address": {
            "type": "string"
        }
    }
  }
}

以上架构是有效架构,但没有创建任何内容。我不知道我是否遗漏了什么,或者使用 jsonschema2pojo 是不可能的。

还有没有办法在运行时生成这些实体?

如果有人对此有任何想法,请分享。

【问题讨论】:

    标签: java json jsonschema jsonschema2pojo


    【解决方案1】:

    解决方案是将它们拆分为单独的文件,或者从另一个文件中引用这些架构,例如:

    {"$ref" : "schemas/myfile.json#AddressDetails"}
    

    谢谢。

    【讨论】:

      【解决方案2】:

      我为此做了一些丑陋的解决方法,让 jsonschema2pojo 生成一个外部“垃圾”对象,其中包含

      "$schema": "http://json-schema.org/draft-04/schema#",
      "description":"Do not use outer object, it's merely a schema wrapper for the inner objects.",
      "id":"http://some-path",
      "type":"object",
      "properties": {
      <all your objects go here>
      }
      

      ...它会给你留下一个垃圾对象,但让你在一个模式中定义所有对象。不过,我也真的很喜欢更清洁的解决方案。

      【讨论】:

        【解决方案3】:

        我使用了this 网站,并且能够从单个 JSON 文件生成多个 POJO。他们还具有 ZIP 选项,可以一次下载所有文件。

        【讨论】:

          猜你喜欢
          • 2019-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-07
          • 1970-01-01
          • 2019-07-24
          • 2017-03-15
          • 2015-04-29
          相关资源
          最近更新 更多