Reputation: 129
I have to get the value of a property from the body of a POST call.
I want to POST a JSON of this type:
{"key" : ["test:testValue"
"keyTest:value"],
"address": "testAddress",
"type" : "street"}
Before to save the entity I want to check if the value of "key" property contains a String with the value which contains the char ":"
- some kind of validation.
At the same time I want to ensure the value of "type" is part of a list of Enumeration - some kind of validation also here.
I tried to use simple()
or exchange
- and validator()
in the way to check if the value contains :
- but without any success.
How I can get the value of a key from the body of the POST call?
Upvotes: 0
Views: 3087
Reputation: 127
A simple solution would be to first unmarshal the JSON to a POJO and then validate the POJO using the Bean Component(https://camel.apache.org/components/latest/bean-component.html).
Example:
.unmarshal().json(JsonLibrary.Jackson, Foo.class)
.bean(new CustomValidator(), "validateFoo")
The CustomValidator can be implemented like this(this is just an example, you can update it depending on you requirements):
public class CustomValidator {
public void validateFoo(Exchange exchange) {
Foo foo = exchange.getIn().getBody(Foo.class);
if (foo == null || !validKeyList(foo.getKey())) {
// throw exception
}
}
private boolean validKeyList(List<String> values) {
for (String value : values) {
if (value.contains(":")) {
return true;
}
}
return false;
}
}
For this to work you would need to add the camel-jackson library (https://mvnrepository.com/artifact/org.apache.camel/camel-jackson).
You can find information about JSON unmarshaling here: https://camel.apache.org/manual/latest/json.html.
Upvotes: 1