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

Eric B.

I'm using Constraint Validators (JSR-303) on my beans in a Spring MVC Controller. Given the following java bean:

public class EntityDTO {

    protected Long id;
    protected Integer version;
    protected String someOtherField;

    // getters and setters
}

I have 2 different types of constraint validation I would like to perform. Either both id and version are null, or both are non-null.

I can create 2 different constraint validators and assign to 2 different annotations: DTOEntityFieldsEmpty & DTOEntityFieldsNotEmpty. But then I have to specify the validator annotation at the bean level.

@DTOEntityFieldsEmpty
public class EntityDTO {
   ....
}    

However, I'm looking to specify the validator that I want to use in the actual controller method level. Under normal circumstances, my method would be:

public void updateData( @RequestBody @Valid EntityDTO dto){
   ...
}

where the @Valid annotation will apply the Validator that is defined in the EntityDTO object. But I'm looking to see if there is a way I can either pass a parameter at the @Valid request, or specify the validator to use.

// @Valid annotation not supported this way
public void updateData( @RequestBody @Valid(validator=DTOEntityFieldsEmpty.class) EntityDTO dto){
   ...
}

Is there anything I can do to get around this? I realize that I can use Spring's @InitBinder, but that will bind a validator to the entire Controller, and not just one specific method.

I've checked both JSR-303 1.0 and 1.1, and don't see anything that jumps out at me to handle this circumstance. Similarly, I can't find anything in the Spring 3 or 4 docs either. I wonder if there might be a way using Group validation, but not entirely sure. I would need to be able to know which validator was successful or failed in the controller, and that seems a little hacky to me.

Ralph

You can use Validation Groupes. Therefore you need to assign the constraints at the fields to groups and then you need to tell spring which group to validate (this can be one or more groupes), therefore Spring 3.1 introduced the @Validated annotation.

public interface GroupA { }  //empty marker interface
public interface GroupB { }


@DTOEntityFieldsEmpty(groups=GroupA.class)
@DTOEntityFieldsNotEmpty(groups=GroupB.class)
public class EntityDTO {
    ....
} 


public void updateData(
       @RequestBody @Validated({ GroupA.class }) EntityDTO dto){
   ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to use Java Bean Validators (JSR-303/JSR-349) on elements of an array/list/collection

From Dev

Exception while binidng form to bean using Spring MVC, JSR 303 Annotation

From Dev

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

From Dev

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

From Dev

Spring JSR-303 bean validation not working, empty bindingresult

From Dev

Spring + JSF2 + JSR 303 bean validation

From Dev

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

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

JSR-303 / Spring MVC - validate conditionally using groups

From Dev

JSR303 custom validators being called twice

From Dev

kotlin data class + bean validation jsr 303

From Dev

JSR-303 Bean Validation Collection Values

From Dev

bean validation (JSR 303) for primefaces not working

From Dev

Better way of using JSR303 annotations in Spring MVC to check whitespaces only

From Dev

Spring MVC custom validators

From Dev

Nested validation groups, Spring, JSR 303

From Dev

Spring Boot JSR-303/349 configuration

From Dev

JSR 303 bean validation unit testing with Mockito and Autowiring

From Dev

Wildfly: ExceptionMapper not triggered with RestEasy JSR-303 Bean Validation

From Dev

Same class for different spring bean instances

From Dev

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

From Dev

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

From Dev

Tests fail with custom jsr validators with @Inject for Spring boot application

From Dev

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

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 boot jsr 303 validation with nested array path fails

Related Related

  1. 1

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

  2. 2

    How to use Java Bean Validators (JSR-303/JSR-349) on elements of an array/list/collection

  3. 3

    Exception while binidng form to bean using Spring MVC, JSR 303 Annotation

  4. 4

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

  5. 5

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

  6. 6

    Spring JSR-303 bean validation not working, empty bindingresult

  7. 7

    Spring + JSF2 + JSR 303 bean validation

  8. 8

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

  9. 9

    Spring mvc controller jsr 303 basic list validation

  10. 10

    Spring mvc controller jsr 303 basic list validation

  11. 11

    JSR-303 / Spring MVC - validate conditionally using groups

  12. 12

    JSR303 custom validators being called twice

  13. 13

    kotlin data class + bean validation jsr 303

  14. 14

    JSR-303 Bean Validation Collection Values

  15. 15

    bean validation (JSR 303) for primefaces not working

  16. 16

    Better way of using JSR303 annotations in Spring MVC to check whitespaces only

  17. 17

    Spring MVC custom validators

  18. 18

    Nested validation groups, Spring, JSR 303

  19. 19

    Spring Boot JSR-303/349 configuration

  20. 20

    JSR 303 bean validation unit testing with Mockito and Autowiring

  21. 21

    Wildfly: ExceptionMapper not triggered with RestEasy JSR-303 Bean Validation

  22. 22

    Same class for different spring bean instances

  23. 23

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

  24. 24

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

  25. 25

    Tests fail with custom jsr validators with @Inject for Spring boot application

  26. 26

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

  27. 27

    Spring Boot JSR303 message code in annotation getting ignored

  28. 28

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

  29. 29

    spring boot jsr 303 validation with nested array path fails

HotTag

Archive