gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:35:36 28 4
gpt4 key购买 nike

我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com