java - Json 架构验证失败,并出现 MalformedURLException : unknown protocol: classpath error

标签 java json jsonschema json-schema-validator

我的 pom.xml 中有以下依赖项

 <!-- https://github.com/everit-org/json-schema -->
               <dependency>
                   <groupId>com.github.everit-org.json-schema</groupId>
                   <artifactId>org.everit.json.schema</artifactId>
                   <version>1.11.1</version>
               </dependency>

               <!-- https://mvnrepository.com/artifact/org.json/json -->
               <dependency>
                   <groupId>org.json</groupId>
                   <artifactId>json</artifactId>
                   <version>20190722</version>
               </dependency>

这是我的 json 架构

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "id": "test",
    "title": "test-json validation",
    "description": "This schema should define the structure of the test json",
    "allOf": [
        {
            "$ref": "classpath:/jsonSchema/header/test1.json#/definitions/test1"
        },
        {
            "$ref": "classpath:/jsonSchema/rows/test2.json#/definitions/test2"
        }
    ],
    "properties": {
        "version": {
            "type": "array",
            "items": {
                "type": "string",
                "enum": [
                    "2.0",
                    "2.1"
                ]
            }
        }
    },
    "required": [
        "version"
    ]
}

这就是我想要实现的目标

public Schema createSchema(String schemaPath) throws IOException {

        Schema schema = null;
        try (InputStream inputStream = new ClassPathResource(schemaPath).getInputStream()) {
            JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
            schema = SchemaLoader.load(rawSchema);
        }
        return schema;
    }

我得到以下异常:

Caused by: java.io.UncheckedIOException: java.net.MalformedURLException: unknown protocol: classpath at org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:20) at org.everit.json.schema.loader.JsonPointerEvaluator.executeWith(JsonPointerEvaluator.java:78) at org.everit.json.schema.loader.JsonPointerEvaluator.lambda$forURL$1(JsonPointerEvaluator.java:121) at org.everit.json.schema.loader.JsonPointerEvaluator.query(JsonPointerEvaluator.java:151) at org.everit.json.schema.loader.ReferenceLookup.lookup(ReferenceLookup.java:173) at org.everit.json.schema.loader.ReferenceSchemaExtractor.extract(SchemaExtractor.java:193) at org.everit.json.schema.loader.AbstractSchemaExtractor.extract(SchemaExtractor.java:113) at org.everit.json.schema.loader.SchemaLoader.runSchemaExtractors(SchemaLoader.java:383) at org.everit.json.schema.loader.SchemaLoader.loadSchemaObject(SchemaLoader.java:360)

我需要设置分辨率范围吗?

最佳答案

我设法通过设置解析范围解决了该问题

    public Schema createSchema(String schemaPath) throws IOException {

        Schema schema = null;

        try (InputStream inputStream = new ClassPathResource(schemaPath).getInputStream()) {
            JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
            SchemaLoader schemaLoader = SchemaLoader.builder()
                    .schemaClient(SchemaClient.classPathAwareClient())
                    .schemaJson(rawSchema)
                    .resolutionScope("classpath://jsonSchema") // setting the default resolution scope
                    .build();
            schema = schemaLoader.load().build();
        }
        return schema;
    }

关于java - Json 架构验证失败,并出现 MalformedURLException : unknown protocol: classpath error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978255/

相关文章:

Python jsonschema 不标记日期时间错误

Java 7u4 webstart 安全异常 : Class does not match trust level

java - 我可以在程序中调用java web start吗?

java - 为什么java文件IO使用字节数据类型?

php - 如何使用 AWS PHP sdk 以 JSON 形式检索 Amazon S3 存储桶的内容?

c++ - 如何在 Objective-C 中验证 JSON Schema?

java - 对于依赖于 Java 中的内容的未覆盖代码,SonarQube 新代码覆盖率的质量门失败

ios - JSONModel - 使用同一 JSONModel 中的其他属性分配 JSONModel 属性的值

javascript - AngularJS 如何访问对象的值

jsonschema - 如何限制 JSON 模式中对象键的最大长度