I got an endpoint looking like this:
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
Event createEvent (@RequestBody(required = true) @Valid EventRequestBody eventRequestBody) {
.....
}
My EventRequestBody looks like this:
class EventRequestBody {
@NotNull
Long start //represents timestamp
@NotNull
Long end //represents timestamp
}
I already told spring boot that both properties are mandatory. But i also want spring boot to check that the start property has to be smaller than the end property. There are some threads out there to do this with custom validation. But all of them are confusing me and i really dont know which way is the most simple and properly working way.
Any suggestions?
If you want to do it with
ConstraintValidator. You should to do something like this: Declare a custom annotation:Also a implementation of this annotation
On your
EventRequestBodyclass add next one annotation@ValidatorValuesThe final result will look:
And on controller/service layer it depends on the situation. you will invoke next one method:
For more details check Validation