0

I have a controller where I have multiple levels of validation combined. The problem is that the validation on 'ToBeValidated' class are executed. But the '@SystemId' validation is skipped because of the '@Validated' annotation on the parameter 'ToBeValidated', when I remove this the opposite is true. Only the cross-parameter validation '@SystemId' is executed. How can I get spring to execute both together?

@Validated
public class Controller {

    @PutMapping(path = THEMA + "/{systemId}", consumes = APPLICATION_JSON_VALUE)
    @SystemId
    public ResponseEntity<Void> update(final @PathVariable("systemId") SystemId systemId,
                                       final @RequestBody @Validated(Update.class) ToBeValidated dto) {

       ....
    }

}

Tried using @Validated on different levels. I want to make both validation work.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Ali
  • 1
  • 1
  • They are executed both but if the `@SystemId` fails it will not continue with the request body validation. This is simply due to different mechanism are used to trigger the validation. – M. Deinum Jun 19 '23 at 07:56
  • @M.Deinum you are almost right. I tested what you said. Apparently the order of execution for the validations in my case is the other way around. De validations on the class level fail and therefore the cross parameter validation is not executed. I just need to make it execute it in the correct order now. Thank you very much, I did not think of this. – Ali Jun 19 '23 at 08:38
  • You cannot change the order, as the `@Validated` on the class is done first (always) as that is done through AOP, where as the `@Validated` on the method argument is part of the execution of the request method. – M. Deinum Jun 19 '23 at 08:55

0 Answers0