Is there a standard way to enable JSR 303 Bean Validation using annotated method arguments
Asked Answered
B

3

10

I've been looking a around for a while now with no luck. I'n not using Spring MVC but still want to use @javax.validation.Valid to enable validation of method arguments. To give an example

public class EventServiceImpl implements IEventService {
    @Override
    public void invite(@Valid Event event, @Valid User user) { ... }
}

Using MVC, this is enabled for @Controller annotated beans with a simple <mvc:annotation-driven/> (see 5.7.4.3 Configuring a JSR-303 Validator for use by Spring MVC).

Using AOP should be quite trivial. Nevertheless, I suspect there's some standard way to do this. Hence the question: Is there a similar thing for non-MVC applications and non-controller beans to enable input validation for annotated beans?

Broucek answered 4/2, 2011 at 16:3 Comment(0)
L
7

Method level validation is not part of the Bean Validation specification (JSR 303). Method level validation is a suggestion in the spec added in appendix C.

Hibernate Validator 4.2 (a beta is out already) is implementing this suggestion and allows to place JSR 303 annotations on method parameters and return values. Of course you will still need some Spring glue code, but that should not be too hard.

Also Bean Validation 1.1 will add method level validation officially to the spec (not just as appendix/recommendation). See also http://beanvalidation.org/

Languish answered 5/2, 2011 at 14:29 Comment(0)
B
4

Using MVC, this is enabled for @Controller annotated beans

@Valid is just a marker in Controller beans that hides the code that does the validation and puts all constraint violations in Errors in a nice way. Spring designers could have invented their own annotation to do the same thing.

The real use of @Valid annotation is in the class (bean) that you are validating with JSR 303 validator and its primary use is to validate the object graph. Meaning one bean can have other bean references with @Valid annotation to trigger validation recursively.

Outside the MVC, you can use configured validator to validate any bean that uses JSR 303 annotations but, unlike the nicely populated Errors in controller, you will have to decide yourself what you are going to do with constraint violations.

So, to answer your question, there is no standard way. To have the same appearance as in a controller, you could use @Valid annotation (or create a new one) to run AOP advice to validate a bean and populate a 'ViolationCollector' (something like Errors in MVC) that must be passed to a method.

Blues answered 5/2, 2011 at 0:32 Comment(0)
U
1

The answers seem to be quite old. As of now, you can utilize @Validated and MethodValidationPostProcessor for method inline validation of any Spring beans. They are basically responsible for creating pointcut-like behavior for Spring managed beans of any tier, not Controllers specifically. Also see my other answer.

Utley answered 11/2, 2019 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.