37

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 ?

5 Answers 5

23

In the eyes of a Spring MVC stack, there is no such thing as a service layer. The reason it works for @Controller class handler methods is that Spring uses a special HandlerMethodArgumentResolver called ModelAttributeMethodProcessor which performs validation before resolving the argument to use in your handler method.

The service layer, as we call it, is just a plain bean with no additional behavior added to it from the MVC (DispatcherServlet) stack. As such you cannot expect any validation from Spring. You need to roll your own, probably with AOP.


With MethodValidationPostProcessor, take a look at the javadoc

Applicable methods have JSR-303 constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation).

Validation groups can be specified through Spring's Validated annotation at the type level of the containing target class, applying to all public service methods of that class. By default, JSR-303 will validate against its default group only.

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. Remember that you can use as many annotations as you would like on a method parameter.

1
14

As a side note on Spring Validation for methods:

Since Spring uses interceptors in its approach, the validation itself is only performed when you're talking to a Bean's method:

When talking to an instance of this bean through the Spring or JSR-303 Validator interfaces, you'll be talking to the default Validator of the underlying ValidatorFactory. This is very convenient in that you don't have to perform yet another call on the factory, assuming that you will almost always use the default Validator anyway.

This is important because if you're trying to implement a validation in such a way for method calls within the class, it won't work. E.g.:

@Autowired
WannaValidate service;
//...
service.callMeOutside(new Form);

@Service
public class WannaValidate {

    /* Spring Validation will work fine when executed from outside, as above */
    @Validated
    public void callMeOutside(@Valid Form form) {
         AnotherForm anotherForm = new AnotherForm(form);
         callMeInside(anotherForm);
    }

    /* Spring Validation won't work for AnotherForm if executed from inner method */
    @Validated
    public void callMeInside(@Valid AnotherForm form) {
         // stuff
    }        
}

Hope someone finds this helpful. Tested with Spring 4.3, so things might be different for other versions.

2
  • 2
    What should we do to make it work when executed from inner method?
    – ammy
    Jun 14, 2020 at 16:51
  • 1
    @ammy Because of how Spring AOP inside of class methods calls can't be caught - read about Proxy pattern to understand this
    – J Asgarov
    Jan 12, 2022 at 14:59
7

@pgiecek You don't need to create a new Annotation. You can use:

@Validated
public class MyClass {

    @Validated({Group1.class})
    public myMethod1(@Valid Foo foo) { ... }

    @Validated({Group2.class})
    public myMethod2(@Valid Foo foo) { ... }

    ...
}
2
  • 2
    also be aware of interface types: MethodValidationInterceptor uses interface type to collect annotations of method. If you have @Validated(Group1.class) on implementation validation for this group will be skipped.
    – pls
    Sep 20, 2016 at 12:40
  • @pls, why would you say that? I'm very much interested in this subject. Can you elaborate? Is it only about the groups or Validated in general? I put Validated on an implementation and along with Valid on the method args it works like a charm.
    – yuranos
    Nov 8, 2017 at 16:26
6

Be careful with rubensa's approach.

This only works when you declare @Valid as the only annotation. When you combine it with other annotations like @NotNull everything except the @Valid will be ignored.

The following will not work and the @NotNull will be ignored:

@Validated
public class MyClass {

    @Validated(Group1.class)
    public void myMethod1(@NotNull @Valid Foo foo) { ... }

    @Validated(Group2.class)
    public void myMethod2(@NotNull @Valid Foo foo) { ... }

}

In combination with other annotations you need to declare the javax.validation.groups.Default Group as well, like this:

@Validated
public class MyClass {

    @Validated({ Default.class, Group1.class })
    public void myMethod1(@NotNull @Valid Foo foo) { ... }

    @Validated({ Default.class, Group2.class })
    public void myMethod2(@NotNull @Valid Foo foo) { ... }

}
1
  • both will not work, because of syntax error :) you forgot about return type
    – Jakub
    Dec 31, 2018 at 12:21
5

As stated above to specify validation groups is possible only through @Validated annotation at class level. However, it is not very convenient since sometimes you have a class containing several methods with the same entity as a parameter but each of which requiring different subset of properties to validate. It was also my case and below you can find several steps to take to solve it.

1) Implement custom annotation that enables to specify validation groups at method level in addition to groups specified through @Validated at class level.

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValidatedGroups {

    Class<?>[] value() default {};
}

2) Extend MethodValidationInterceptor and override determineValidationGroups method as follows.

@Override
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
    final Class<?>[] classLevelGroups = super.determineValidationGroups(invocation);

    final ValidatedGroups validatedGroups = AnnotationUtils.findAnnotation(
            invocation.getMethod(), ValidatedGroups.class);

    final Class<?>[] methodLevelGroups = validatedGroups != null ? validatedGroups.value() : new Class<?>[0];
    if (methodLevelGroups.length == 0) {
        return classLevelGroups;
    }

    final int newLength = classLevelGroups.length + methodLevelGroups.length;
    final Class<?>[] mergedGroups = Arrays.copyOf(classLevelGroups, newLength);
    System.arraycopy(methodLevelGroups, 0, mergedGroups, classLevelGroups.length, methodLevelGroups.length);

    return mergedGroups;
}

3) Implement your own MethodValidationPostProcessor (just copy the Spring one) and in the method afterPropertiesSet use validation interceptor implemented in step 2.

@Override
public void afterPropertiesSet() throws Exception {
    Pointcut pointcut = new AnnotationMatchingPointcut(Validated.class, true);
    Advice advice = (this.validator != null ? new ValidatedGroupsAwareMethodValidationInterceptor(this.validator) :
            new ValidatedGroupsAwareMethodValidationInterceptor());
    this.advisor = new DefaultPointcutAdvisor(pointcut, advice);
}

4) Register your validation post processor instead of Spring one.

<bean class="my.package.ValidatedGroupsAwareMethodValidationPostProcessor"/> 

That's it. Now you can use it as follows.

@Validated(groups = Group1.class)   
public class MyClass {

    @ValidatedGroups(Group2.class)
    public myMethod1(Foo foo) { ... }

    public myMethod2(Foo foo) { ... }

    ...
}
1
  • Thanks for sharing. However is this still relevant, given the other answers use @Validated at the method level, and specify groups?
    – user2964500
    Dec 9, 2016 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.