【问题标题】:Specifying which validation group to use for a bean指定用于 bean 的验证组
【发布时间】:2017-02-23 21:13:37
【问题描述】:

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

我正在尝试使here 描述的解决方案如下工作。但是没有遇到违反约束的情况,事情就过去了。为什么?

我有两个豆子,一个是父母,另一个是孩子。子定义如下

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


}

测试类如

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



}

顺便说一句,我已经设置了适当的弹簧级别 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();
    }
}

【问题讨论】:

  • 您所说的“没有组也可以正常工作”是什么意思?我试图重现您的场景,并从 SampleBean 和 SampleBeanParent 中删除了与组相关的代码,但这也导致“成功完成”
  • 我添加了注释语句,以显示属于默认值的所有约束在正常情况下工作正常的情况。只需取消注释它们并评论与组相关的行

标签: java spring bean-validation spring-validator validationgroup


【解决方案1】:

我可以通过以下方式做到这一点。更改的类如下(我从代码中删除了从特定问题的角度来看似乎是多余/不必要的所有内容)

@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,它包含几个有用的点,其中引用自其中一个答案:

“@Validated 注解仅用于指定一个验证组,它本身并不强制进行任何验证。您需要使用 javax.validation 批注之一,例如 @Null 或 @Valid。” - 表明您的示例中的@Validated(Group1.class) SampleBean childBean 似乎不正确

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

【讨论】:

  • 其他答案的链接对我帮助很大:)。我希望有一种方法可以指定用于特定方法参数的验证组。看起来只能在类和方法级别完成,不能在方法参数级别完成。
猜你喜欢
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
相关资源
最近更新 更多