3

我正在查看来自 github 的 2.2.6 版本的验证器代码。我没有更改回购“ https://github.com/fge/json-schema-validator.git ”中的任何代码

当我针对引用第二个模式文件的 json 模式进行测试时,我无法运行示例 1(当我使用硬编码的 URI 时,我可以让它工作)。

我只是简单地重新指出了“com.github.fge.jsonschema.examples.Example1.java”以使用我的团队 json 模式和 json 文件。我已经构建了项目并将我的 json 模式文件复制到“json-schema-validator\bin\com\github\fge\jsonschema\examples”(都在同一个文件夹中,类似于 fstab 示例)

附上一段顶层,

               },
                "MovingWindow": {
                    "description": "Is this an moving window measure?",
                    "type": "boolean"
                }
            },
            "minItems": 1,
            "uniqueItems": true
        },
        "RealTimeProfile": {
            "$ref": "rtp.json#"
        }
    },
    "required": [
        "MeasureTemplateId",
        "MeasureInstanceId",

但我无法读取较低级别的第二个模式文件(“rtp.json”)以被识别并正常工作。我看到以下错误:

线程“main”com.github.fge.jsonschema.core.exceptions.ProcessingException 中的异常:致命:URI“rtp.json#”不是绝对级别:“致命”uri:“rtp.json#”

我的代码片段:

File jsonFile = new File("CumulativeCountBad.json");
File jsonSchemaFile = new File("main.json");


JsonNode good = JsonLoader.fromFile(jsonFile);
JsonNode mainSchema = JsonLoader.fromFile(jsonSchemaFile);

final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

final JsonSchema schema = factory.getJsonSchema(mainSchema);

ProcessingReport report;

report = schema.validate(good);
System.out.println("good: " + report);

我的问题似乎类似于以下问题,但是当我将引用设置为时,我似乎无法让事情运行:“$ref”:“rtp.json#”

https://github.com/fge/json-schema-validator/issues/94

任何帮助表示赞赏。PS - 我是一个java新手,如果有什么明显的东西我已经忽略了,我们深表歉意谢谢

4

3 回答 3

8

问题是您加载 JSON 然后将其转换为模式。并且您的架构在“id”中没有绝对 URI。所以,它不能工作。

您想使用绝对 URI 来加载它们。由于您最初使用 a File(注意,对于 Java 7+,您真的想使用 java.nio.file 代替),您可以通过以下方式获取它的绝对 URI:

final File jsonSchemaFile = new File("main.json");
final URI uri = jsonSchemaFile.toURI();

然后,您使用以下命令加载架构:

final JsonSchema schema = factory.getJsonSchema(uri.toString());
于 2014-12-16T17:52:25.237 回答
0

我刚刚将我的参考从 Style 1 更改"$ref":"Booking.json" 为 Style 2 :"$ref":"resource:/booking-schemas/Booking.json"

它对我有用。可以使用将 json 生成到 java pojo 文件,"$ref":"Booking.json" 但是在读取 json 有效负载时 style1 失败。我只是将其更改为样式 2,它在两种情况下都对我有用。

于 2021-04-11T11:45:07.043 回答
0

用 . 更新模式 json 文件"$id": "/",。在此更改之后,模式 json 将具有类似于以下的条目:

{ 
   "$schema": "http://json-schema.org/draft-04/schema",
   "$id": "/",
   "title":"Your title",
   "description": "Your description",
   ...
}

这对我有用。

于 2021-07-30T12:43:55.780 回答