是否有使用带注释的方法参数启用 JSR 303 Bean 验证的标准方法 [英] Is there a standard way to enable JSR 303 Bean Validation using annotated method arguments
问题描述
我已经四处张望了一段时间,但没有运气.我不使用 Spring MVC,但仍想使用 @javax.validation.Valid
来启用方法参数的验证.举个例子
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) { ... }
}
使用 MVC,通过一个简单的 <mvc:annotation-driven/>
(参见 5.7.4.3 配置JSR-303供 Spring MVC 使用的验证器).
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).
使用 AOP 应该很简单.尽管如此,我怀疑有一些标准的方法可以做到这一点.因此,问题是:对于非 MVC 应用程序和非控制器 bean 是否有类似的事情来为带注释的 bean 启用输入验证?
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?
推荐答案
方法级别验证不是 Bean 验证规范 (JSR 303) 的一部分.方法级别验证是在 附录 C 中添加的规范中的建议.
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(测试版已经出来)正在实施这个建议,并允许在方法参数和返回值上放置 JSR 303 注释.当然,您仍然需要一些 Spring 胶水代码,但这应该不会太难.
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.
此外,Bean Validation 1.1 将正式将方法级别验证添加到规范中(不仅仅是作为附录/推荐).另请参阅 http://beanvalidation.org/
Also Bean Validation 1.1 will add method level validation officially to the spec (not just as appendix/recommendation). See also http://beanvalidation.org/
这篇关于是否有使用带注释的方法参数启用 JSR 303 Bean 验证的标准方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!