-1

사양에 사용 : 최대 절전 모드 - 유효성 검사기 [5.2.4.Final, 스프링 컨텍스트 [4.2.2.RELEASE]지정은 빈

내가 솔루션으로 here 작업을 설명하기 위해 노력하고 이하. 그러나 아무런 제약 조건 위반이 발생하지 않고 & 것들이 단지 잘 통과합니다. 왜?

두 콩, 한 부모, 다른 자식이 있습니다. 그런데

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



} 

, 난 설정은 적절 한 봄 레벨을 가지고

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


} 

테스트 클래스는 다음과 같습니다 부모 bean 정의는 다음과 같습니다

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

} 

아래로 아이의 정의는 콩으로 아래 &로 잘 작동합니다. (위의 코드에서 작업 케이스에 대한 그룹이없는 유효성 검사 행에 주석을 달았습니다.) 그래서 이것은 문제가되지 않습니다 :)

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

그리고"그룹 없이도 잘 작동합니까? " 귀하의 시나리오를 재현 해 보았고 SampleBean과 SampleBeanParent에서 그룹 관련 코드를 제거하려고 시도했지만 "성공적으로 완료되었습니다"도 나타났습니다 – hammerfest

+0

기본으로 속한 모든 제약 조건의 정상적인 사례를 보여주기 위해 주석 문을 추가했습니다. 그룹의 관련 댓글을 주석 처리하고 댓글을 달으십시오. – nikel

답변

0

나는 그렇게 할 수 있습니다. 그것은 몇 가지 유용한 점을 포함, Spring @Validated in service layer : 변경된 클래스는

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

} 

는이 스레드를 참조하십시오 (내가보기의 특별한 문제 점에서 불필요/중복 것 같았다 코드에 이르기까지 모든 것을 제거)은 다음과 같습니다

"@Validated 주석은 유효성 검사 그룹을 지정하는 데만 사용되며 자체적으로 유효성 검사를 수행하지는 않습니다. javax.validation 주석 중 하나를 사용해야합니다 (예 : @ Null 또는 @Valid. " - 귀하의 예제에서 @Validated(Group1.class) SampleBean childBean이 올바른 것으로 보이지 않는다는 것을 나타냅니다.

마지막 답변은 @Valid 이외의 메소드 매개 변수에 또 다른 주석이있을 때와 같은 특정 사례에 대해 논의하고 있습니다. @NotNull (message = " 자식은 null이 될 수 없습니다. ")

+0

다른 답변의 링크는 나에게 많은 도움이되었습니다. :). 특정 메서드 매개 변수에 사용할 유효성 검사 그룹을 지정하는 방법이 있기를 희망했습니다. 클래스 및 메서드 수준에서만 수행 할 수 있고 메서드 매개 변수 수준에서는 수행 할 수없는 것처럼 보입니다. – nikel

관련 문제