骆驼 json 验证器支持 json 草案 07 [英] Camel json validator support for json draft 07
问题描述
我们使用 Apache Camel Java DSL 和 json-validator 组件根据 json 模式验证 json 请求.当前的camel 版本是2.22.0,根据camel 文档,它使用NetworkNT JSON Schema 库支持JSON 模式v4、v6、v7 和v2019-09.但是,当我尝试 JSON 架构草案 07 时,运行测试时出现错误由:com.networknt.schema.JsonSchemaException: Unknown Metaschema: http://json-schema.org/draft-07/schema#".
We are using Apache Camel Java DSL and the json-validator component to validate json requests against a json schema. The current camel version is 2.22.0, according to the camel documentation this supports JSON schema v4, v6, v7 and v2019-09 using the NetworkNT JSON Schema library. However, when I try a JSON schema draft 07, I get an error when running tests "Caused by: com.networknt.schema.JsonSchemaException: Unknown Metaschema: http://json-schema.org/draft-07/schema#".
当我恢复到 json 模式草案 04 时,它工作正常.
When i revert back to json schema draft 04 it works fine.
关于如何让它工作的任何想法?
Any ideas on how to get this working?
推荐答案
默认草稿是第 4 个,但您可以通过定义一个 bean 来覆盖模式验证器 (JsonSchemaLoader).
The default draft is the 4th one, but you can override the schema validator (JsonSchemaLoader), by defining a bean.
@Bean(name = "mySchemaLoader")
public JsonSchemaLoader mySchemaLoader() {
return (camelContext, schemaStream) -> JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)
.getSchema(schemaStream);
}
如果你想覆盖默认配置,我们只需创建一个 bean,它将返回一个 V7 模式验证器
with that we just create a bean wich will return a V7 schema validator, if you want to override the default configuration
...
ObjectMapper mapper = new ObjectMapper();
JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
....
所以有了 bean,你只需要告诉骆驼你将通过查询参数使用那个 bean
So having the bean, you just have to said to camel that you are going to usea that bean via query param
public void configureRemote() throws Exception {
from("direct:getPrescripciones")
.recipientList(
simple"${header.url}?bridgeEndpoint=true"))
.to("json-validator:deliveryReport.schema.json?schemaLoader=#bean:mySchemaLoader")
.end();
}
仅此而已这里是我使用的依赖项
that's all here the dependencies I used
<properties>
<java.version>1.8</java.version>
<camel.version>3.4.0</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-json-validator-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jolt</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-swagger-java-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-google-pubsub-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-google-pubsub</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
这篇关于骆驼 json 验证器支持 json 草案 07的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!