0
votes

I am learning apache camel, I have created one spring boot project in which i want to validate the message body with json schema. In my project, I have create route in which i am fetching file from the location in my computer and then passing it to next endpoint where i have applied json validator component and then passed to seda endpoint where i consumed the output.

The code is as follow :

@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();

    }

}

the test.json file content is :

{
  "id" :1
 }

and myschema.json file content is :

{
  "id" :1
 } 

I placed the myschema.json file in src/main/resources folder

I am getting error :

Message History (complete message history is disabled)

RouteId ProcessorId Processor Elapsed (ms) [route1 ] [route1 ] [from[file://C:%5CsourceFolder?fileName=test.json&noop=true] ] [ 2] ... [route1 ] [to1 ] [json-validator:classpath:myschema.json ] [ 0]

Stacktrace

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

1
do u have camel-json-validator-starter dependency in pom?pvpkiran

1 Answers

0
votes

Either this is a copy/paste mistake or your schema file is not a JSON schema, but the same as your json data.

Take a look at the Camel docs for an example of a JSON schema file that can be used with the validator.