0

As far as i know, in JEE7 Bean Validation is automatically integrated with CDI. For example if i use a CDI container, i don't need to inject and use a javax.validation.Validator to check if my bean violate some constraint. So, i don't need to do something like this:

@Inject
Validator validator;

... 

SoccerPlayer player = new SoccerPlayer();
    player.setFirstName(firstName);
    player.setLastName(lastName);
    player.setAge(age);

Set<ConstraintViolation<SoccerPlayer>> violations = validator
            .validate(player);

But the following SoccerPlayer bean

public class SoccerPlayer {

@NotNull
@Size(min = 5)
private String firstName;

@NotNull
@Size(min = 5)
private String lastName;

@Max(50)
@Min(value=16, groups = GoalKeeper.class)
private int age;

private String position;

public SoccerPlayer() {
}

// Getters and setters omitted 
}

could automatically validate with @Valid annotation.

public class SoccerPlayerProcessor {    

    public void processPlayer(@Valid SoccerPlayer player){
        // Do stuff with the player.
    }   
}

Now in this case i can't use groups in conjuction with @Valid annotation, to diversify validation behaviour.

So, is there any method to achieve my goal? Or as an alternative, could i disable CDI integration with bean validation? Could i override default CDI javax.validation.Validator with my custom implementation?

I'm using WebSphere Liberty profile as application server.

Thank you in advance.

EDIT:

To be more accurate, this is an extract from: https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-integration.html#section-integration-with-cdi

As of version 1.1, Bean Validation is integrated with CDI (Contexts and Dependency Injection for JavaTM EE).

This integration provides CDI managed beans for Validator and ValidatorFactory and enables dependency injection in constraint validators as well as custom message interpolators, traversable resolvers, constraint validator factories and parameter name providers.

Furthermore, parameter and return value constraints on the methods and constructors of CDI managed beans will automatically be validated upon invocation.

When your application runs on a Jave EE container, this integration is enabled by default.

My question is, is there any method to disable this default behaviour? Or in alternative can i override default CDI interceptor? Or is it impossible?

mancioshell
  • 27
  • 2
  • 10
  • What do you mean by "i can't use groups in conjuction with `@Valid` annotation"? – Rouliboy Feb 18 '17 at 20:01
  • I mean what about if i would like to validate SoccerPlayer with GoalKeeper.class group? AFAIK i can't do something like @Valid(groups=GoalKeeper.class). If i could override default Validator, i could annotate my API with custom annotation (for example Validate which has groups field @Validate(groups=GoalKeeper.class)) and in the Validator i could collect all the bean annotated with it and perform validation with specific group. – mancioshell Feb 19 '17 at 09:42

1 Answers1

2

Try using @ConvertGroup to achieve the desired outcome.

Building on your previous example the code would look like this.

public class SoccerPlayerProcessor {    

    public void processPlayer(@Valid @ConvertGroup(from=Default.class, to=GoalKeeper.class) SoccerPlayer player){
        // Do stuff with the player.
    }   

If you need more than one @ConvertGroup, use @ConvertGroup.List to hold multiple @ConvertGroup annotations for the same element.

  • I would recommend to use validator.forExecutables().validateParameters(..) method in your unit test to check, if your validation works as it should. – Petr Hunka Mar 24 '17 at 15:30