java 是否有使用带注释的方法参数启用 JSR 303 Bean 验证的标准方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4900077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a standard way to enable JSR 303 Bean Validation using annotated method arguments
提问by sfussenegger
I've been looking a around for a while now with no luck. I'n notusing Spring MVC but still want to use @javax.validation.Valid
to enable validation of method arguments. To give an example
我已经环顾了一段时间,但没有运气。我没有使用 Spring MVC,但仍想使用它@javax.validation.Valid
来启用方法参数的验证。举个例子
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).
使用 MVC,这可以@Controller
通过简单的注释 bean启用<mvc:annotation-driven/>
(请参阅5.7.4.3 配置 JSR-303 验证器以供 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?
使用 AOP 应该很简单。尽管如此,我怀疑有一些标准的方法可以做到这一点。因此,问题是:对于非 MVC 应用程序和非控制器 bean 是否有类似的事情来为带注释的 bean 启用输入验证?
采纳答案by Hardy
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.
方法级别验证不是 Bean 验证规范 (JSR 303) 的一部分。方法级别验证是附录 C 中添加的规范中的建议。
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.
Hibernate Validator 4.2(测试版已经出来)正在实施这个建议,并允许在方法参数和返回值上放置 JSR 303 注释。当然,您仍然需要一些 Spring 胶水代码,但这应该不会太难。
Also Bean Validation 1.1 will add method level validation officially to the spec (not just as appendix/recommendation). See also http://beanvalidation.org/
此外,Bean Validation 1.1 将正式将方法级别验证添加到规范中(不仅仅是作为附录/推荐)。另见http://beanvalidation.org/
回答by Ritesh
Using MVC, this is enabled for @Controller annotated beans
使用 MVC,这是为 @Controller 注释 bean 启用的
@Valid is just a marker in Controller beans that hides the code that does the validation and puts all constraint violations in Errors
in a nice way. Spring designers could have invented their own annotation to do the same thing.
@Valid 只是控制器 bean 中的一个标记,它隐藏了执行验证的代码,并Errors
以一种很好的方式将所有违反约束的情况放入。Spring 设计者可以发明他们自己的注解来做同样的事情。
The real use of @Valid annotation is in the class (bean) that you are validating with JSR 303 validator and its primary use is to validate the object graph. Meaning one bean can have other bean references with @Valid annotation to trigger validation recursively.
@Valid 注释的真正用途是在您使用 JSR 303 验证器验证的类(bean)中,它的主要用途是验证对象图。这意味着一个 bean 可以具有其他 bean 引用,并带有 @Valid 注释以递归触发验证。
Outside the MVC, you can use configured validator to validate any bean that uses JSR 303 annotations but, unlike the nicely populated Errors
in controller, you will have to decide yourself what you are going to do with constraint violations.
在 MVC 之外,您可以使用配置的验证器来验证使用 JSR 303 注释的任何 bean,但是,与Errors
在控制器中很好地填充不同,您必须自己决定要对约束违规做什么。
So, to answer your question, there is no standard way. To have the same appearance as in a controller, you could use @Valid annotation (or create a new one) to run AOP advice to validate a bean and populate a 'ViolationCollector' (something like Errors in MVC) that must be passed to a method.
所以,要回答你的问题,没有标准的方法。要具有与控制器相同的外观,您可以使用 @Valid 注释(或创建一个新注释)来运行 AOP 建议以验证 bean 并填充必须传递给方法。
回答by yuranos
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.
答案似乎很旧。到目前为止,您可以利用@Validated
和MethodValidationPostProcessor
对任何 Spring bean 进行方法内联验证。它们基本上负责为任何层的 Spring 管理 bean 创建类似切入点的行为,而不是专门为控制器创建。另请参阅我的其他答案。