I had a similar problem lately, but regarding RESTDsl received parameters
validations.

The solution was to build a POJO from the Exchange headers somehow and use
the bean-validator as mentioned above. (We used Dozer to automatically
convert header Map to our POJO class with fields matching the parameters.)

On Tue, Dec 12, 2017 at 2:39 PM, Richard James <[email protected]>
wrote:

> Thanks Steve, that has worked nicely with the REST Api.
>
> I have been able to use that endpoint to do the necessary validation,
> including identifying null values. I have also been able to implement more
> custom validation e.g. specifying expected string length.
>
> Thank you for pointing me in the right direction, it has helped to cure a
> few hours frustration! :)
>
> On Tue, Dec 12, 2017 at 6:33 PM, Steve Huston <[email protected]>
> wrote:
>
> > I don't have a REST-specific example to show you, but in other scenarios
> > I've done, after unmarshalling to POJO:
> >
> > .to("bean-validator://validate-request")
> >
> > If the validator annotations find a violation an exception will be
> thrown.
> >
> > -Steve
> >
> > > -----Original Message-----
> > > From: Richard James [mailto:[email protected]]
> > > Sent: Tuesday, December 12, 2017 1:19 PM
> > > To: [email protected]
> > > Subject: Camel REST Spring Boot Pojo Mandatory Json Properties
> > >
> > > 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).enableCO
> > > RS(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
> >
>

Reply via email to