-1

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();
    }
}
Community
  • 1
  • 1
nikel
  • 3,402
  • 11
  • 45
  • 71
  • and what do you mean by "it works fine without groups"? I tried to reproduce your scenario, and removed group related code from SampleBean and SampleBeanParent, but that resulted in "succesfully finished" too – hammerfest Oct 14 '16 at 13:25
  • i have added comment statements to show the normal case of all constraints belonging to default where it works fine. Just uncomment them & comment the group related lines – nikel Oct 14 '16 at 13:32

1 Answers1

0

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

Community
  • 1
  • 1
hammerfest
  • 2,203
  • 1
  • 20
  • 43
  • The Link to other answer helped me a lot :). I was hoping there would be a way to specify which validation groups to use for a particular method parameter. Looks like it can only be done at the class & method level and Not at the method parameter level. – nikel Oct 15 '16 at 03:32