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?
JSR-303 standardizes validation constraint declaration and metadata for the Java platform. Using this API, you annotate domain model properties with declarative validation constraints and the runtime enforces them. There are a number of built-in constraints you can can take advantage of.
The @Valid annotation applies validation rules on the provided object. The BindingResult interface contains the result of validation.
The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With