10

Is JSR-303 also intended for method parameter validation?

If so, is there any example on the web? The biggest challenge I'm facing is how to get a validator within each method. With Spring 3, doesn't it mean that I'd have to inject virtually every class with a LocalValidatorFactoryBean?

Thanks!

Tom Tucker
  • 11,676
  • 22
  • 89
  • 130

5 Answers5

10

Method level validation will be supported in the upcoming version 4.2 of JSR 303's reference implementation Hibernate Validator.

As pointed out in the previous answer this will allow constraint annotations (built-in as well as custom defined types) to be specified at method parameters and return values to specify pre- and post-conditions for a method's invocation.

Note that HV will only deal with the actual validation of method calls not with triggering such a validation. Validation invocation could be done using AOP or other method interception facilities such as JDK's dynamic proxies.

The JIRA issue for this is HV-347 [1], so you might want to add yourself as watcher to this issue.

[1] http://opensource.atlassian.com/projects/hibernate/browse/HV-347

Gunnar
  • 266
  • 2
  • 4
  • I think your blog post supplements your answer ;) http://musingsofaprogrammingaddict.blogspot.com/2011/01/method-validation-with-hibernate.html – Maxym Jan 27 '11 at 15:34
  • Gunnar was nice enough to put his solution into [GitHub](https://github.com/gunnarmorling/methodvalidation-integration). Worked great for me. – Patrick Oct 13 '11 at 21:24
3

In Spring 3.1.0 you can use @Validated annotation to activate validation on a pojo. Create an interface for the pojo class an put this annotation over it, then add your validation annotations in the methods definitions. ( the interface is required because Spring will create a proxy class using the interface as definition )

@Validated
public interface PojoClass  {
    public @NotNull String doSomething(@NotEmpty String input);
}

your pojo :

public class PojoClassImpl implements PojoClass {
    public String doSomething(String input) {
        return "";
    }
}

Starting from a standard spring web application with active validation, remember to add in your spring configuration this bean declaration:

<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
Massimo
  • 1,012
  • 14
  • 23
  • For some weird reason ConstraintViolationException public \@NotNull String doSomething(); didn't work but \@NotNull public String doSomething(); did. Used the bar to escape the At symbol that was confussed with user by S.O – borjab Apr 18 '16 at 15:10
3

The javadocs for each of the JSR 303 annotations tells the following:

@Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER})

See, PARAMETER is there. So, yes, it's technically possible.

Here's an example:

public void method(@NotNull String parameter) {
    // ...
}

I'm however not sure how that integrates with Spring since I don't use it. You could just give it a try.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

This sounds like a use case for AOP (AspectJ). Write a pointcut for methods that are annotated with javax.validation.constraints.*, inject a validator into the aspect (probably using Spring) and use a @Before or @Around advice to perform the validation before method execution.

Read AspectJ in Action for reference.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

You can use jcabi-aspects (I'm a developer), which integrates JSR 303 with AspectJ. Actual validation is done by one of JSR 303 implementations, which you will have to add to class path, like Hibernate Validator, or Apache BVal.

yegor256
  • 102,010
  • 123
  • 446
  • 597