【发布时间】:2015-10-01 22:46:42
【问题描述】:
您好,我在 2.2.6 版中使用 JSON Schema evaluator 来验证我的服务器响应。这些响应可以包含 A、B 或 C 类型的单个对象,也可以包含复合对象,例如包含 A 对象数组的 D。为了重用每个对象的模式定义,我开始在同一个文件中描述所有实体,如here 所述。现在我的问题是,在验证响应时我必须引用其中一个对象。
这是我的(不是)SWE。
JSON 模式文件:
{
"id":"#root",
"properties": {
"objecta": {
"type": "object",
"id":"#objecta",
"properties": {
"attribute1": {"type": "integer"},
"attribute2": {"type": "null"},
},
"required": ["attribute1", "attribute2"]
},
"objectb": {
"type": "object",
"id":"#objectb",
"properties": {
"attribute1": {"type": "integer"},
"attribute2": {
"type": "array",
"items": {
"$ref": "#/objecta"
}
}
}
},
"required": ["attribute1", "attribute2"]
},
}
}
现在我想验证包含对象 B 的服务器响应。为此,我尝试了以下方法:
public class SchemeValidator {
public static void main(String[] args) {
String jsonData = pseudoCodeFileLoad("exampleresponse/objectb.txt");
final File jsonSchemaFile = new File("resources/jsonschemes/completescheme.json");
final URI uri = jsonSchemaFile.toURI();
ProcessingReport report = null;
try {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(uri.toString() + "#objectb");
JsonNode data = JsonLoader.fromString(jsonData);
report = schema.validate(data);
} catch (JsonParseException jpex) {
// ... handle parsing errors etc.
}
}
}
问题是方案没有正确加载。我要么没有错误(即使响应无效),要么我得到fatal: illegalJsonRef,因为该方案似乎是空的。如何在我的 Java 代码中使用对象 b 的模式?谢谢!!
【问题讨论】:
标签: java json schema jsonschema