【问题标题】:Json schema validation fails with MalformedURLException: unknown protocol: classpath errorJson 模式验证失败并出现 MalformedURLException:未知协议:类路径错误
【发布时间】:2025-12-16 07:15:01
【问题描述】:

我的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;
    }

我得到以下异常:

原因:java.io.UncheckedIOException:java.net.MalformedURLException:未知协议:类路径 在 org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:20) 在 org.everit.json.schema.loader.JsonPointerEvaluator.executeWith(JsonPointerEvaluator.java:78) 在 org.everit.json.schema.loader.JsonPointerEvaluator.lambda$forURL$1(JsonPointerEvaluator.java:121) 在 org.everit.json.schema.loader.JsonPointerEvaluator.query(JsonPointerEvaluator.java:151) 在 org.everit.json.schema.loader.ReferenceLookup.lookup(ReferenceLookup.java:173) 在 org.everit.json.schema.loader.ReferenceSchemaExtractor.extract(SchemaExtractor.java:193) 在 org.everit.json.schema.loader.AbstractSchemaExtractor.extract(SchemaExtractor.java:113) 在 org.everit.json.schema.loader.SchemaLoader.runSchemaExtractors(SchemaLoader.java:383) 在 org.everit.json.schema.loader.SchemaLoader.loadSchemaObject(SchemaLoader.java:360)

我需要设置resolution 范围吗?

【问题讨论】:

    标签: java json jsonschema json-schema-validator


    【解决方案1】:

    我通过设置resolution scope设法解决了这个问题

        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;
        }
    

    【讨论】:

      最近更新 更多