Java 如何为@Valid 指定验证组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18911154/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:15:25  来源:igfitidea点击:

How to specify validation group for @Valid?

javaspringspring-mvccontroller

提问by

I get so parameters in @Controller @RequestMapping method:

我在@Controller @RequestMapping 方法中得到这样的参数:

@ModelAttribute("myCandidate") @Valid Candidate myCandidate,
BindingResult result

How can I explicit specify validation group for myCandidate ?

如何为 myCandidate 显式指定验证组?

采纳答案by John Farrelly

The standard java @Validannotation doesn't support groups. However, Spring now includes an @Validatedannotation which does the same job as @Valid, but allows you to specify which groups to use in the validation:

标准的 java@Valid注释不支持组。但是,Spring 现在包含一个@Validated注释,它与 完成相同的工作@Valid,但允许您指定在验证中使用哪些组:

@ModelAttribute("myCandidate") @Validated(UpdateGroup.class) Candidate myCandidate

Note that this annotation is only available in Spring 3.1 and newer.

请注意,此注释仅在 Spring 3.1 及更新版本中可用。