I have a controller with a post method that takes in some headers as string as well as a request body as an object.
I want to use spring validation for all of it. I currently have my headers validation inline, and my request object has validation annotations all throughout its objects and fields. I am currently using a globalExceptionHandler to catch the different exceptions such as MethodArgumentNotValid and ConstraintsViolation.
The validation works perfectly, but I need to put some order to it, I would prefer to have:
1.headers validation 2.general request validation (not null and not blank) 3.detailed request validation (patterns and sizes)
I've seen people use the groups field in the validation annotation to group their validations, and they create their sequences using the @GroupSequence annotation on an interface of their choice. I want to implement something of this sort, but it only seems to work when you use a ValidatorFactory and validator.validate, not with the exceptions.
Any help would be appreciated.
I can take the tedious route and order all of my messages by priority and just sort through them, but I want this solution to be scalable to multiple projects with different sets of errors.
I currently have the @Validated and @GroupSequence({Controller.class, headers.class, request1.class, request2.class}) on my controller class, then inline I have @NotBlank for my headers and @Valid for my object. then inside my object I have @valid for my cascading objects and an assortment of different validations all throughout. the validations work, just not in the order specified in the group sequence. I can still get to the last group of validations with an error on the first layer.
using hibernate.validator version 5.2.4.final.