When I am validating bean using @Valid
annotation in javax.validation
, for some objects I am getting ConstraintViolationException
and for some I am getting a MethodArgumentNotValidException
.
I understand that, if I validate anything in @ResponseBody
in the controller , it throws a MethodArgumentNotValidException
.
But for some custom validations(eg. @MyCustomValidation
) at the class level it is throwing ConstraintViolationException
even if it is being validated in @ResponseValidation.
And for some other custom validation for a different REST endpoint, it throws MethodArgumentNotValidException
.
I am finding it a bit difficult to understand its behavior.
@PostMapping(path = "/someEndPoint")
@Validated(OnASave.class)
public ResponseEntity<ClassA> saveObjA(@Valid @RequestBody ClassA objA)
Result - throws
MethodArgumentNotValidException
@PostMapping(path = "/someOtherEndPoint")
@Validated(OnBSave.class)
public ResponseEntity<ClassB> saveObjB(@Valid @RequestBody ClassB objB)
Result - throws
ConstraintViolationException
Both ClassA
and ClassB
has custom validations.
ClassA
andClassB
with import statements, there is a possibility ofConstraintViolationException
which is part of jsr 380 – Algophobia