Manually call Spring Annotation Validation

后端 未结 6 1685
太阳男子
太阳男子 2020-11-29 02:42

I\'m doing a lot of our validation with Hibernate and Spring Annotations like so:

public class Account {
    @NotEmpty(groups = {Step1.class, Step2.class})
          


        
6条回答
  •  一生所求
    2020-11-29 03:17

    Adding to answered by @digitaljoel, you can throw the ConstraintViolationException once you got the set of violations.

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            Set> violations = validator.validate( notionalProviderPaymentDTO );
    
            if(!violations.isEmpty()) {
                throw new ConstraintViolationException(violations);
            }
    

    You can create your own exception mapper which will handle ConstraintViolationException and send the errors messages to the client.

提交回复
热议问题