Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 917
sfussenegger Avatar asked Feb 04 '11 16:02

sfussenegger


People also ask

In which implementation is the JSR 303 standard used?

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.

Which annotations is used for validation?

The @Valid annotation applies validation rules on the provided object. The BindingResult interface contains the result of validation.

What does @NotNull annotation mean in bean property?

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.


1 Answers

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.

like image 122
yuranos Avatar answered Oct 05 '22 02:10

yuranos