How to combine JSR-303 and Spring Validator class in a service layer?

Ivan

I have some model class

public class Account {

    @Email
    private String email;

    @NotNull
    private String rule;
}

and spring-validator

public class AccountValidator implements Validator {

    @Override
    public boolean supports(Class aClass) {
        return Account.class.equals(aClass);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        Account account = (Account) obj;
        ValidationUtils.rejectIfEmpty(errors, "email", "email.required");
        ValidationUtils.rejectIfEmpty(errors, "rule", "rule.required");

        complexValidateRule(account.getRule(), errors);
    }

    private void complexValidateRule(String rule, Errors errors) {
        // ...
    }
}

I run in my service

AccountValidator validator = new AccountValidator();
Errors errors = new BeanPropertyBindingResult(account, "account");
validator.validate(account, errors);

Can I add to my validation process constraints @Email, @NotNull (JSR-303) and don't describe these rules in AccountValidator?

I know how works @Valid in spring-controllers, but what's about service layer? Is it possible? How to do such kind of validation in a proper way? May I should use Hibernate Validator?

matthias

Spring provides an Adapter to merge both validation APIs. See the current Spring JavaDoc for more information.

An possible implementation would be

public class AccountValidator implements Validator {

  private final SpringValidatorAdapter validator;

  public AccountValidator(SpringValidatorAdapter validator) {
      super();
      this.validator = validator;
  }

  @Override
  public boolean supports(Class aClass) {
      return Account.class.equals(aClass);
  }

  @Override
  public void validate(Object obj, Errors errors) {

      //jsr303
      validator.validate(obj, errors);

      //custom rules
      Account account = (Account) obj;
      complexValidateRule(account.getRule(), errors);
  }

  private void complexValidateRule(String rule, Errors errors) {
      // ...
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Validate different model class into one form using Spring MVC JSR-303 Validator

From Dev

How to resolve dependencies in JSR-303 custom validator

From Dev

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

From Dev

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

From Dev

How to break coupling between bean and validator with Bean Validation (JSR303)?

From Java

How to call a java class method from spring boot service layer

From Dev

Custom Bean Validator -- JSR303/JSR349 can Validator choose message when invalid?

From Dev

kotlin data class + bean validation jsr 303

From Dev

Nested validation groups, Spring, JSR 303

From Dev

Spring Boot JSR-303/349 configuration

From Dev

Conditional JSR-303 validation based on enclosed class type

From Dev

Can JSR 303 Bean Validation be used with Spring Data Rest?

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

Spring Boot JSR303 message code in annotation getting ignored

From Dev

JSR-303 errors not being resolved by spring:message tag

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

Spring JSR-303 bean validation not working, empty bindingresult

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

Spring + JSF2 + JSR 303 bean validation

From Dev

JSR-303 / Spring MVC - validate conditionally using groups

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

spring boot jsr 303 validation with nested array path fails

From Dev

BindingResult not working in Spring 3.2.8 using JSR-303 BEAN VALIDATION

From Dev

How to authorize the service layer of a Spring application?

From Dev

Spring MVC - How to create a proper Service layer?

From Dev

How to handle version of Spring JPA in Service Layer

From Dev

How to isolate Spring service/persistence layer from JSF view layer

From Dev

How to implement multiple JSR-303 validation messages for the same @Constraint?

From Dev

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

Related Related

  1. 1

    How to Validate different model class into one form using Spring MVC JSR-303 Validator

  2. 2

    How to resolve dependencies in JSR-303 custom validator

  3. 3

    Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

  4. 4

    Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

  5. 5

    How to break coupling between bean and validator with Bean Validation (JSR303)?

  6. 6

    How to call a java class method from spring boot service layer

  7. 7

    Custom Bean Validator -- JSR303/JSR349 can Validator choose message when invalid?

  8. 8

    kotlin data class + bean validation jsr 303

  9. 9

    Nested validation groups, Spring, JSR 303

  10. 10

    Spring Boot JSR-303/349 configuration

  11. 11

    Conditional JSR-303 validation based on enclosed class type

  12. 12

    Can JSR 303 Bean Validation be used with Spring Data Rest?

  13. 13

    Spring MVC: use different JSR-303 validators on the same bean?

  14. 14

    Spring Boot JSR303 message code in annotation getting ignored

  15. 15

    JSR-303 errors not being resolved by spring:message tag

  16. 16

    Spring mvc controller jsr 303 basic list validation

  17. 17

    Spring JSR-303 bean validation not working, empty bindingresult

  18. 18

    Spring mvc controller jsr 303 basic list validation

  19. 19

    Spring + JSF2 + JSR 303 bean validation

  20. 20

    JSR-303 / Spring MVC - validate conditionally using groups

  21. 21

    Spring MVC: use different JSR-303 validators on the same bean?

  22. 22

    spring boot jsr 303 validation with nested array path fails

  23. 23

    BindingResult not working in Spring 3.2.8 using JSR-303 BEAN VALIDATION

  24. 24

    How to authorize the service layer of a Spring application?

  25. 25

    Spring MVC - How to create a proper Service layer?

  26. 26

    How to handle version of Spring JPA in Service Layer

  27. 27

    How to isolate Spring service/persistence layer from JSF view layer

  28. 28

    How to implement multiple JSR-303 validation messages for the same @Constraint?

  29. 29

    Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

HotTag

Archive