java json架构验证相对路径不起作用(未找到URI) [英] java json schema validation relative path not working (URI not found)

查看:125
本文介绍了java json架构验证相对路径不起作用(未找到URI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking at the 2.2.6 version of the validator code from github. I have not changed any code from the repo "https://github.com/fge/json-schema-validator.git"

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

I am unable to get example 1 running when i test it against my json schema that references a second schema file (I can get it working when I resort to a hardcoded URI).

我只是重新命名了com.github.fge.jsonschema.examples.Example1.java来使用我的团队json架构和json文件。
我已经构建了项目并将我的json模式文件复制到json-schema-validator\bin\com\github\fge\jsonschema \ examples中(全部在同一个文件夹中,类似于fstab示例)

I have simply repointed the "com.github.fge.jsonschema.examples.Example1.java" to use my teams json schema and json files. I have built the project and copied my json schema files into "json-schema-validator\bin\com\github\fge\jsonschema\examples" (All in the same folder, similar to fstab examples)

附上顶级部分,

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

但是我无法获得较低级别的读数,第二个模式文件(rtp.json)被识别出来&工作正常。
我看到以下错误:

but I am unable to get the reading of the lower level, 2nd schema file ("rtp.json") to be recognised & work correctly. I am seeing the below error:

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

Exception in thread "main" com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: URI "rtp.json#" is not absolute level: "fatal" 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);

我的问题似乎与下面的问题相似,但是当我设置时,我似乎无法得到运行的东西e引用:
$ ref:rtp.json#

My issue seems similar to the following issue but i don't seem to be able to gett the thing running when i set the reference to: "$ref": "rtp.json#"

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

任何帮助表示赞赏。
PS - 我是一个java新手,道歉,如果有明显的东西,我已经省了
谢谢

Any help appreciated. PS - I am a java newbie, apologies if there is something obvious that i have ommited Thanks

推荐答案

<问题是您加载JSON然后将其转换为架构。并且您的架构在id中没有绝对URI。因此,它无法工作。

The problem is that you load the JSON then turn it into a schema. And your schema does not have an absolute URI in "id". So, it cannot work.

您希望使用绝对URI来加载它们。由于您最初使用的是文件(注意,使用Java 7+,您确实希望使用java.nio.file),您可以使用以下方法获取绝对URI: / p>

You want to use an absolute URI to load them. Since you originally use a File (note, with Java 7+ you really want to use java.nio.file instead), you can obtain an absolute URI to it with:

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

然后加载架构:

final JsonSchema schema = factory.getJsonSchema(uri.toString());

这篇关于java json架构验证相对路径不起作用(未找到URI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆