将json模式中的ref用于pojo时出错

时间:2015-05-27 19:45:13

标签: java jsonschema jsonschema2pojo

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

Device.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
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

感谢here,joelittlejohn的回答。我使用spec.json

中的 javaType 来实现此功能

Device.json

function_name = ...

Spec.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"
}