Rob
Rob

Reputation: 1328

Apache Camel: Validate RESTful JSON input based on databse base content

Summary

A RESTful POST request POST /request/{requestName}

POST /request/CreateProduct

{
    "Code": 4711,
    "Name": "My product"
}

is to be validated:

  1. if the given ${header.requestName} has a corresponding row in the database and
  2. if the provided parameters match the requestName according to another table in the database, specifially if all required params are present and have the correct data type

Current route sample config

restConfiguration().component("netty4-http").port(8080).bindingMode(RestBindingMode.json);

rest("request/{requestName}").post()
    .consumes("application/json; charset=UTF-8")
    .produces("application/json; charset=UTF-8")
    .to("direct:newRequest");

from("direct:newRequest").transform().simple("Received request: ${header.requestName}, Body: ${in.body}");

DB tables (MariaDB) to validate against

Table: request

id | name
------------------
 1 | CreateProduct
 2 | UpdateProduct
 3 | DeleteProduct

Table: request_parameter

id | name | type
-------------------
 1 | Code  | INT
 2 | Name  | STRING
 2 | Price | INT

Table request_to_parameter

request | paramater | required
------------------------------
      1 |         1 |        1
      1 |         2 |        1
      1 |         3 |        0

Question

Is this possible with pure Camel? Or should I implement my own helper function? How to I include my own custom function in a Camel route?

Upvotes: 0

Views: 1126

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36163

You can implement a customer processor as described here: http://camel.apache.org/processor.html

With the Exchange object you have access to header and body and can extract the necessary information to validate.

Upvotes: 1

Related Questions