【问题标题】:Error in using ref in json schema to pojo在 json 模式中使用 ref 到 pojo 时出错
【发布时间】:2015-08-10 02:09:53
【问题描述】:

我正在尝试使用jsonschema2pojo 来生成Java 类。我在使用“$ref”标签来引用父架构中的其他架构时遇到了麻烦。


设备.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "device:v1",
    "name": "device",
    "properties": {
        "components": {
            "$ref": "spec.json"
            },
        "usage": {
            "$ref": "spec.json"
            }
    }
},
"required": true,
"title": "Device",
"type": "object"
}

这是我的 spec.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "spec:v1",
    "name": "spec",
    "properties": {
        "content": {
            "description" : "Content",
            "type": "string",
            "required": false
    }
},
"required": true,
"title": "spec",
"type": "object"
}

现在我希望创建以下 java 类:

public class Device {
    private Spec components,
    private Spec usage

    .....
}

public class Spec {
    private String content
}

但我明白了

public class Device {
    private Components components,
    private Components usage

    .....
}

public class Components {
    private String content
}

我做错了什么?

【问题讨论】:

    标签: java jsonschema jsonschema2pojo


    【解决方案1】:

    感谢 joelittlejohn 的 here 的回答。我使用 spec.json 中的 javaType 让这个工作


    设备.json

    {
        "$schema": "http://json-schema.org/draft-03/hyper-schema",
        "additionalProperties": false,
        "id": "device:v1",
        "name": "device",
        "properties": {
            "components": {
                "$ref": "spec.json",
                "type": "object",
                },
            "usage": {
                "$ref": "spec.json",
                "type": "object",
                }
        }
    },
    "required": true,
    "title": "Device",
    "type": "object"
    }
    

    Spec.json

    {
        "$schema": "http://json-schema.org/draft-03/hyper-schema",
        "additionalProperties": false,
        "javaType": "my-package-name.Spec"
        "id": "spec:v1",
        "name": "spec",
        "properties": {
            "content": {
                "description" : "Content",
                "type": "string",
                "required": false
        }
    },
    "required": true,
    "title": "spec",
    "type": "object"
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2014-07-08
      • 2016-10-28
      • 2011-09-19
      相关资源
      最近更新 更多