Hej,
I want to use the @Validated(group=Foo.class)
annotation to validate an argument before executing a method like following:
public void doFoo(Foo @Validated(groups=Foo.class) foo){}
When i put this method in the Controller of my Spring application, the @Validated
is executed and throws an error when the Foo object is not valid. However if I put the same thing in a method in the Service layer of my application, the validation is not executed and the method just runs even when the Foo object isn't valid.
Can't you use the @Validated
annotation in the service layer ? Or do I have to do configure something extra to make it work ?
Update:
I have added the following two beans to my service.xml:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
and replaced the @Validate
with @Null
like so:
public void doFoo(Foo @Null(groups=Foo.class) foo){}
I know it is a pretty silly annotation to do but I wanted to check that if I call the method now and passing null it would throw an violation exception which it does. So why does it execute the @Null
annotation and not the @Validate
annotation ? I know one is from javax.validation
and the other is from Spring but I do not think that has anything to do with it ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…