【问题标题】:Pojo to Json SchemaPojo 到 Json 架构
【发布时间】:2020-10-14 07:50:53
【问题描述】:

我有下面的 Json 字符串

{
"name": "Mr Xyz"
}

上述 JSON 的 Java 模型类如下所示

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name"
})
public class Test {

@JsonProperty("name")
@JsonPropertyDescription("Non Zero Length String")
@Size(min = 1)
@NotNull
private String name;

以下 Test.class Java 对象的 Jackson API 生成以下 json 模式

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Test",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1,
            "description": "Non Zero Length String"
        }
    },
    "required": [
        "name"
    ]
}

我的问题是如何更改我的 Java pojo 对象,以便它可以生成如下所示的架构

{
    "$id": "test id",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "Test title",
    "description": "test description",
    "type": "object",
    "properties": {
        "name": {
            "$ref": "#/definitions/d_NonZeroLengthString"
        }
    },
    "definitions": {
        "d_NonZeroLengthString": {
            "description": "Non Zero Length String",
            "type": "string",
            "minLength": 1
        }
    }
}

【问题讨论】:

  • 您为什么希望 Jackson 创建定义?
  • @OneCricketeer 因为我无法将我的 json 消息发送到 Kafka 模式注册表代理,因为它说模式无效,即使我的 json 消息是正确的
  • 这个工具可能有帮助:jsonschema2pojo.org

标签: java json jackson jsonschema


【解决方案1】:

尝试使用https://github.com/mbknor/mbknor-jackson-jsonSchema, 它创建“$ref”和“定义”

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2021-08-22
    • 2020-07-30
    • 2017-09-08
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多