开发者

Method Parameter Validation with JSR-303

开发者 https://www.devze.com 2023-01-30 00:31 出处:网络
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

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!


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


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.


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"/>


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消