使用Everit进行JSON验证时的java外部引用产生“FileNotFoundException”
我试图添加JSON模式验证,以清理Java应用程序的用户输入。所有模式都存储在进行验证的文件系统中。我的模式是这样的:
{
"$id": "https://www.my-url.org/schemas/schemaName.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Train Approximation",
"type": "object",
"properties":{
"settings":{
"type":"object",
"oneOf":[
{"$ref":"https://www.my-url.org/anotherSchema.json"}
]
}
}
}
(在“其中之一”中还有其他项目,以及其他不相关的属性和描述。)
我正在使用Everit库进行模式验证。我检查模式的代码如下所示:
String[] schemaNames = {"schemaName.json","anotherSchema.json", "moreSchemasEtc.json"};
String schemaFile = schemaName + ".json";
File fSchemaFile = new File(dir.getAbsoluteFile() + File.separator + schemaFile);
if (fSchemaFile.exists()) {
try (InputStream inputStream = new FileInputStream(fSchemaFile)) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
SchemaLoaderBuilder slb = SchemaLoader.builder()
.schemaClient(SchemaClient.classPathAwareClient())
.schemaJson(rawSchema)
.resolutionScope("classpath://Schema");
for(String schemaFileName : schemaNames) {
File tSchemaFile = new File(dir.getAbsoluteFile() + File.separator + schemaFileName);
InputStream inStream = new FileInputStream(tSchemaFile);
JSONObject rawTSchema = new JSONObject(new JSONTokener(inStream));
slb.registerSchemaByURI(new URI("https://www.my-url.org/schemas/"+schemaFileName), rawTSchema);
}
SchemaLoader schemaLoader = slb.build();
Schema schema = schemaLoader.load().build();
schema.validate(new JSONObject(jsonString));
} catch (ValidationException e) {
//Do error handling
}
} else { /* Do other stuff */ }
这适用于我所有没有外部引用的模式。(例如:"$ref":"https://www.my-url.org/anotherSchema.json"
)对于我在这里发布的模式,我得到一个带有引用路径的java.io.FileNotFoundException
我做错了什么?我也试过:
-使用类路径感知客户端(仍在上面的代码中),将我的/Schema/
目录上方的父目录添加到我的Java类路径,然后将所有内容引用为"$ref":"anotherSchema.json"
,而不是使用整个路径
-在解析范围中使用my-url.org
路径而不是类路径
使用classpath选项,我得到一个错误,在FileNotFoundException中有一个类似classpath://Schema/anotherSchema.json
的路径,但仍然有一个异常
共 (0) 个答案