28 September 2018

Using camel json schema validation in Talend Studio

Schema validation is well known for XML content. Since JSON has become very popular in combination with REST services the need for JSON schema validation has also increased.

JSON schema validation is available since Camel 2.20.0 matching Talend Studio version 7.0.1.

This article describes how JSON schema validation can be used for Talend Studio route development.

Missing dependencies

The first challenge to take is that the required dependencies are not pre-packed within Talend Studio 7.0.1. Adding the required jar libraries can become quite a challenge if you search for one missing dependency after the other.

Getting a list of all required dependencies can be done a lot easier:

Option A

Copy pom.xml from github and store it as a local file.
Execute Maven to get the full dependency tree:
mvn dependency:tree

Option B

Clone the existing camel repository
git clone https://github.com/apache/camel.git
Checkout to the correct version
git checkout camel-2.20.1 --
Switch to the camel-json-validator component directory
cd camel/components/camel-json-validator
Execute Maven to get the full dependency tree:
mvn dependency:tree


Now you could download each jar file from the dependency list and add it as a remote library to your route within the cConfig component. This would still be a lot of work. See next section on how this can be done easier.

Create JAR file including all dependencies

To avoid downloading and adding each dependency separately you can also create a jar file including all required dependencies within the same jar file. This makes it a lot easier to handle within Studio.

To do this you have to modify the existing pom file and add another plugin for this purpose:

camel/components/camel-json-validator/pom.xml
<project>
  . . .
  <dependencies>
  . . .
  </dependencies>
  <build>
    <plugins>
      <!-- Maven Assembly Plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase> 
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
Exclude camel-core (as this will be already available within the runtime by default) from dependencies by setting scope to provided:
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-core</artifactId>
  <scope>provided</scope>
</dependency>
Execute maven to build jar file:
camel/components/camel-json-validator> mvn clean package


Setup demo route

Import camel/components/camel-json-validator/target/camel-json-validator-2.20.1-jar-with-dependencies.jar within cConfig component of your route in studio.


Before deploy to external runtime check external jar in Bundle ClassPath under the Dependencies tab of your route to include this library as embedded to your route.


Use a generic cMessagingEndpoint component for validation. Set URI to your schema validation file.
E.g.: "json-validator:file:/C:/Talend/jsonValidation/people_schema.json"
Review the full component documentation to learn more about URI syntax and optional parameters if needed.


You can catch the org.apache.camel.ValidationException to handle any payload that is not compliant with the schema.


Run your test route:


You can download the complete sample from my google drive share.

3 comments:

  1. You have worked to perfection on this article. Thanks for taking the time to post search valuable information. I Recommendation this. JSON Beautifier

    ReplyDelete
  2. How I can use json validator in rest service and return json detail error in the body

    ReplyDelete
  3. Free Windows 7 Product Key 2022 · CTPG3-H28YT-JP7WD-TTJ3T-RYHKG · RGQ3V-MCMTC-6HP8R-98CDK-VP3FM · MVYTY-QP8R7-6G6WG-87MGT-CRH2P · MM7DF-.Windows 7 Professional Product Key Free

    ReplyDelete