【问题标题】:How to validate message body with json schema using apache camel in spring boot application如何在 Spring Boot 应用程序中使用 Apache Camel 使用 json 模式验证消息体
【发布时间】:2020-03-23 13:51:58
【问题描述】:

我正在学习 apache camel,我创建了一个 spring boot 项目,我想在其中使用 json 模式验证消息正文。在我的项目中,我创建了路由,在该路由中我从计算机中的位置获取文件,然后将其传递到应用 json 验证器组件的下一个端点,然后传递到我使用输出的 seda 端点。

代码如下:

@SpringBootApplication
public class ExampleCamelDemoApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleCamelDemoApplication.class, args);


        CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {

            public void configure() throws Exception {

//
                from("file:C:\\sourceFolder?fileName=test.json&noop=true").convertBodyTo(String.class).to("json-validator:classpath:myschema.json")
                .to("seda:end");
            }

        });
        context.start();
        ConsumerTemplate ct = context.createConsumerTemplate();
        System.out.println(ct.receiveBody("seda:end"));
        Thread.sleep(10000);
        context.stop();

    }

}

test.json 文件内容为:

{
  "id" :1
 }

而 myschema.json 文件内容为:

{
  "id" :1
 } 

我将 myschema.json 文件放在 src/main/resources 文件夹中

我收到错误:

消息历史(完整的消息历史被禁用)

RouteId ProcessorId 处理器已用(毫秒) [route1] [route1] [来自[file://C:%5CsourceFolder?fileName=test.json&noop=true]] [2] ... [route1] [to1] [json-validator:classpath:myschema.json] [0]

堆栈跟踪

java.lang.NullPointerException: null 在 com.networknt.schema.JsonSchema.combineCurrentUriWithIds(JsonSchema.java:87) ~[json-schema-validator-1.0.29.jar:na] 在 com.networknt.schema.JsonSchema.(JsonSchema.java:75) ~[json-schema-validator-1.0.29.jar:na] 在 com.networknt.schema.JsonSchema.(JsonSchema.java:62) ~[json-schema-validator-1.0.29.jar:na] 在 com.networknt.schema.JsonSchema.(JsonSchema.java:57) ~[json-schema-validator-1.0.29.jar:na] 在 com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:253) ~[json-schema-validator-1.0.29.jar:na]

【问题讨论】:

  • 你在 pom 中有 camel-json-validator-starter 依赖吗?

标签: json spring-boot apache-camel


【解决方案1】:

这要么是复制/粘贴错误,要么是您的架构文件不是 JSON 架构,但与您的 json 数据相同。

查看Camel docs 以获取可与验证器一起使用的 JSON 架构文件的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    相关资源
    最近更新 更多