【问题标题】:declare java.util.Map into json-schema将 java.util.Map 声明为 json-schema
【发布时间】:2016-08-24 13:34:06
【问题描述】:

我需要将java.util.Map 实例映射到org.jsonschema2pojo maven 插件用来创建POJO 的JSON 模式。

我没有找到一个好的简单的解决方案。

有人可以帮帮我吗?

这是我实际的 json-schema 文件

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Response",
    "description": "A Response object",
    "type": "object",
    "properties": {
        "result": {
            "type": "string",
            "description": "describes response status"
        },
        "msg": {
            "type": "string",
            "description": "user msgs"
        }
    },
    "required": ["result"],
    "additionalProperties": false
}

我需要添加一个字段"errors",该字段在Java中转换为java.util.Map<String, String>。

【问题讨论】:

  • 您能否详细说明您的问题?据我了解,您有一个描述的 JSON 模式,并且您的地图可能看起来像:“errors”:{“type”:“string”},然后您想将地图添加到您的 JSON 模式文件中并使用它在 jsonschema2pojo 中,对吗?如果我有任何误解,请举个例子,谢谢!

标签: java json maven jsonschema jsonschema2pojo


【解决方案1】:

AFAIK additionalProperties 完成了这项工作。 您可以声明 Map<String, Object> 类型的错误属性,例如像这样(现在是 yaml):

...
properties:
  errors:
    type: object
    additionalProperties:
      type: object

您无需指定键的类型,因为它描述了一个 json 文档,它自然地将字符串作为对象的键。

除了type: object,您还可以为Map<String, String> 执行type: string,或者如果您在该映射中有自己的类型作为值,请引用另一个定义。

【讨论】:

  • 嗨!谢谢您的回复。我找到并尝试了additionalProperties,但我无法更改名称然后进入生成的pojo,字段“additionalProperties”与@JsonIgnore 映射,我不想要它。
  • 嗯,可能是代码生成器的一个怪癖。你的意思是什么名字?您能否粘贴一段您尝试过的内容以及从中生成的内容的摘录?
猜你喜欢
  • 2019-09-09
  • 2021-03-08
  • 2018-08-04
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2015-01-29
相关资源
最近更新 更多