-1

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

ここで説明するソリューションを以下のように機能させようとして います。しかし、制約違反は発生せず、問題なく通過します。なんで?

私は 2 つの Bean を持っています。1 つは親、もう 1 つは子です。子の定義は次のとおりです。

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

}

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



}

ちなみに、私は以下のように適切なSpring Level 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();
    }
}
4

1 に答える 1

0

次の方法でそれを行うことができました。変更されたクラスは次のとおりです(特定の問題の観点から冗長/不要と思われるコードからすべてを削除しました)

@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には、いくつかの有用なポイントが含まれており、特に回答の 1 つから引用されています。

「@Validated アノテーションは、検証グループを指定するためにのみ使用されます。それ自体は検証を強制しません。@Null や @Valid などの javax.validation アノテーションのいずれかを使用する必要があります。」-@Validated(Group1.class) SampleBean childBeanあなたの例では正しくないように見えることを示します

最後の答えは、 @NotNull(message = "child cannot be null") もあったように、 @Valid 以外のメソッドパラメーターに別の注釈がある場合の特定のケースについて説明しています

于 2016-10-14T14:35:18.493 に答える