Hi,
I am in the process of implementing a REST Api using Spring boot and Camel.
This is up and running and works well.
What I am currently trying to enforce are some mandatory properties within
the POJO/JSON. I have attempted a number of different ways including using
bean validator (@NotNull) as well as Jackson annotations
(@JsonProperty((value = "departmentCode", required=true). However a call to
the API without one of the mandatory fields works without throwing any
error.
The one way I have got it to work is by putting a check in the
getDepartmentCode method for a null value which then throws a custom
exception that I have created. I'm not sure if this is the best way to do
it.
if (departmentCode!= null) {
return departmentCode;
} else {
throw new CustomJSONException("Department code is a required field");
}
My rest dsl looks like the following;
rest("").bindingMode(RestBindingMode.json).consumes("application/json").produces("application/json")
.post("jobcompletionevent").type(Job.class).outType(String.class).enableCORS(true)
.description("Adds a job ").id("AddJob").responseMessage().code(400)
.message("Invalid JSON
Request").endResponseMessage().responseMessage().code(200)
.message("Valid Request
Received").endResponseMessage().to("direct:jobEventDistributor");
Can anyone point me in the right direction of how I should aim to handle
this use case? Should this be done in the initial binding or as an extra
step afterwards. Any pointers are much appreciated.
Regards,
Richard