指定用于 bean 的验证组

Specifying which validation group to use for a bean

规格:hibernate-validator[5.2.4.Final]、spring-context[4.2.2.RELEASE]

我正在尝试使 here 描述的解决方案按如下方式工作。但是没有遇到违反约束的情况,一切都很好。为什么?

我有两个豆子,一个 parent ,另一个 child。 child定义如下

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;
    }

}

Parent bean定义如下:

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");
    }


}

测试class如

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");
        }
    }



}

顺便说一句,我已经设置了适当的 spring 级别 bean,如下所示,它在没有组的情况下也能正常工作(我已经在上面的代码中评论了验证行,没有针对工作案例的组)。所以这不是问题:)

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();
    }
}

我可以通过以下方式做到这一点。修改后的类如下(我去掉了代码中从具体问题来看似乎是redundant/unnecessary的所有内容)

@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;
    }

}

请同时参考这个帖子:Spring @Validated in service layer,它包含几个有用的要点,其中引用了其中一个答案:

"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." - 表示您的示例中的 @Validated(Group1.class) SampleBean childBean 似乎不正确

最后一个答案是讨论特定情况,当方法参数上除了 @Valid 之外还有另一个注释时,就像在您的情况下还有 @NotNull(message = "child cannot be null")