javaspringbean-validationspring-validatorvalidationgroup

Specifying which validation group to use for a bean


Specs : hibernate-validator[5.2.4.Final], spring-context[4.2.2.RELEASE]

I am trying to make the solution described here work as below. But there are no constraint violations encountered & things just pass by fine. Why?

I have two beans, one parent , other child. The child definition is as below

package code;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@Service("SampleBean")
@Validated
public class SampleBean {

    @NotNull(message= "value can not be null" , groups = Group1.class)
//  @NotNull(message= "value can not be null")
    private Integer value;

    @NotNull(message= "value1 can not be null" , groups = Group2.class)
//  @NotNull(message= "value can not be null" )
    private Integer value1;

    public Integer getValue() {
        return value;
    }

    public void setValue(@NotNull 
            Integer value) {
        this.value = value;
    }

    public Integer getValue1() {
        return value1;
    }

    public void setValue1(Integer value1) {
        this.value1 = value1;
    }

}

The Parent bean definition is as below :

package code;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@Service("SampleBeanParent")
@Validated
public class SampleBeanParent {


    public void acceptChildBean(@NotNull(message = "child cannot be null") 
//  @Valid
    @Validated(Group1.class)
    SampleBean childBean) throws NoSuchMethodException, SecurityException{
        System.out.println("successfully finished");
    }


}

The test class is as

package code;
import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {


    public static void main(String[] args) throws NoSuchMethodException, SecurityException{


        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        SampleBean sampleBean = (SampleBean) context.getBean("SampleBean");

        try{
            SampleBeanParent parent = (SampleBeanParent) context.getBean("SampleBeanParent");
            parent.acceptChildBean(sampleBean);

        }
        catch(ConstraintViolationException e){
            System.out.println("there were validation errors");
        }
    }



}

By The way, i have setup appropriate spring level beans as below & it works fine without groups(i have commented the validation lines in above code without groups for the working case). So this is not the problem :)

package code;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

@Configuration
@ComponentScan(basePackageClasses = {SampleBean.class})
public class SpringConfiguration {

    @Bean(name = "validator")
    public LocalValidatorFactoryBean initValidatorFactory(){
        return new LocalValidatorFactoryBean();
    }

    @Bean
    public MethodValidationPostProcessor initValidationPostProcessor(){
        return new MethodValidationPostProcessor();
    }
}

Solution

  • I could do that the following way. The changed classes are as follows (I removed everything from the code that seemed to be redundant/unnecessary from the particular problem point of view)

    @Service("SampleBeanParent")
    @Validated(Group1.class)
    public class SampleBeanParent {
    
        public void acceptChildBean(
                @Valid SampleBean childBean) throws NoSuchMethodException, SecurityException {
            System.out.println("successfully finished");
        }
    
    }
    
    @Service("SampleBean")
    public class SampleBean {
    
        @NotNull(message = "value can not be null", groups = Group1.class)
        private Integer value;
    
        public Integer getValue() {
            return value;
        }
    
        public void setValue(Integer value) {
            this.value = value;
        }
    
    }
    

    Please also refer to this thread: Spring @Validated in service layer, it contains several useful points, among others quoted from one of the answers:

    "The @Validated annotation is only used to specify a validation group, it doesn't itself force any validation. You need to use one of the javax.validation annotations, like @Null or @Valid." - indicating that @Validated(Group1.class) SampleBean childBean in your example does not seem to be correct

    The last answer is discussing the specific case when there is another annotation on the method parameter besides @Valid like in your case there was also @NotNull(message = "child cannot be null")