【问题标题】:java json schema validation relative path not working (URI not found)java json模式验证相对路径不起作用(找不到URI)
【发布时间】:2014-12-15 10:32:01
【问题描述】:

我正在查看来自 github 的 2.2.6 版本的验证器代码。 我没有更改 repo "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新手,如果我有明显的遗漏,我深表歉意 谢谢

【问题讨论】:

    标签: java json validation github json-schema-validator


    【解决方案1】:

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

    您想使用绝对 URI 来加载它们。由于您最初使用 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());
    

    【讨论】:

    【解决方案2】:

    我刚刚更改了我的参考 从 风格一:"$ref":"Booking.json" 到 风格二:"$ref":"resource:/booking-schemas/Booking.json"

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

    【讨论】:

      【解决方案3】:

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

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

      这对我有用。

      【讨论】:

        猜你喜欢
        • 2018-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-12
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        相关资源
        最近更新 更多