rocky
rocky

Reputation: 831

How to validate the JSON Request in camel rest

I need to validate incoming request to the camel rest service based on some schema. for example.

In request as given below

{
 "routeId" : "fileBatchRoute",
 "action" : "start",
 "sourceLocation" : "sourceDirectory",
 "destinationLocation" : "destinationDirectory"
}

Above request should be validated based on below conditions 1. It must contain action element and format should be above. 2. RouteId should be present.

Upvotes: 2

Views: 4908

Answers (1)

Bedla
Bedla

Reputation: 4919

You can use json-validator component. With schema generation can help you tool JSONschema.net.


With your requirements (routeId is required, action is required and is one of "start", "stop", "suspend", "resume") could schema be something like:

routeSchema.json:

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": [
    "routeId",
    "action"
  ],
  "properties": {
    "routeId": {
      "type": "string"
    },
    "action": {
      "type": "string",
      "enum": [
        "start",
        "stop",
        "suspend",
        "resume"
      ]
    },
    "sourceLocation": {
      "type": "string"
    },
    "destinationLocation": {
      "type": "string"
    }
  }
}

Route definition:

.to("json-validator:routeSchema.json")

Upvotes: 4

Related Questions