Camel json 验证器支持 json 草稿 07 [英] Camel json validator support for json draft 07

查看:21
本文介绍了Camel json 验证器支持 json 草稿 07的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 Apache Camel Java DSL 和 json-validator 组件来根据 json 模式验证 json 请求.当前的骆驼版本是 2.22.0,根据骆驼文档,这支持使用 NetworkNT JSON 模式库的 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,你只需要对camel说你将通过查询参数使用那个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>

这篇关于Camel json 验证器支持 json 草稿 07的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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