Java Spring中@Valid和@Validated的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36173332/
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
Difference between @Valid and @Validated in Spring
提问by Sergei Tachenov
Spring supports two different validation methods: Spring validation and JSR-303 bean validation. Both can be used by defining a Spring validator that delegates to other delegators including the bean validator. So far so good.
Spring 支持两种不同的验证方法:Spring 验证和 JSR-303 bean 验证。两者都可以通过定义委托给其他委托者(包括 bean 验证器)的 Spring 验证器来使用。到现在为止还挺好。
But when annotating methods to actually request validation, it's another story. I can annotate like this
但是当注释方法以实际请求验证时,这是另一回事。我可以这样注释
@RequestMapping(value = "/object", method = RequestMethod.POST)
public @ResponseBody TestObject create(@Valid @RequestBody TestObject obj, BindingResult result) {
or like this
或者像这样
@RequestMapping(value = "/object", method = RequestMethod.POST)
public @ResponseBody TestObject create(@Validated @RequestBody TestObject obj, BindingResult result) {
Here, @Valid is javax.validation.Valid, and @Validated is org.springframework.validation.annotation.Validated. The docs for the latter say
这里,@Valid是javax.validation.Valid,@Validated 是org.springframework.validation.annotation.Validated。后者的文档说
Variant of JSR-303's Valid, supporting the specification of validation groups. Designed for convenient use with Spring's JSR-303 support but not JSR-303 specific.
JSR-303 的 Valid 变体,支持验证组的规范。专为方便使用 Spring 的 JSR-303 支持而设计,但不是特定于 JSR-303 的。
which doesn't help much because it doesn't tell exactly how it's different. If at all. Both seem to be working pretty fine for me.
这没有多大帮助,因为它并不能确切说明它的不同之处。如果有的话。两者对我来说似乎都很好。
采纳答案by Franti?ek Hartman
As you quoted from the documentation, @Validated
was added to support "validation groups", i.e. group of fields in the validated bean. This can be used in multi step forms where you may validate name, email, etc.. in first step and then other fields in following step(s).
正如您从文档中引用的那样,@Validated
已添加以支持“验证组”,即已验证 bean 中的一组字段。这可用于多步骤表单,您可以在第一步中验证姓名、电子邮件等,然后在后续步骤中验证其他字段。
The reason why this wasn't added into @Valid
annotation is because that it is standardized using the java community process (JSR-303), which takes time and Spring developers wanted to allow people to use this functionality sooner.
之所以没有将其添加到@Valid
注释中,是因为它使用 java 社区流程 (JSR-303) 进行了标准化,这需要时间,并且 Spring 开发人员希望允许人们更快地使用此功能。
Go to this jira ticketto see how the annotation came into existence.
转到此 jira 票证以了解注释是如何产生的。
回答by zhuhang.jasper
A more straight forward answer. For those who still don't know what on earth is "validation group".
一个更直接的答案。对于那些仍然不知道到底什么是“验证组”的人。
Usage for @Valid
Validation
用于@Valid
验证
Controller:
控制器:
@RequestMapping(value = "createAccount")
public String stepOne(@Valid Account account) {...}
Form object:
表单对象:
public class Account {
@NotBlank
private String username;
@Email
@NotBlank
private String email;
}
Usage for @Validated
Validation Group
Source: http://blog.codeleak.pl/2014/08/validation-groups-in-spring-mvc.html
对于使用@Validated
验证组
来源:http://blog.codeleak.pl/2014/08/validation-groups-in-spring-mvc.html
Controller:
控制器:
@RequestMapping(value = "stepOne")
public String stepOne(@Validated(Account.ValidationStepOne.class) Account account) {...}
@RequestMapping(value = "stepTwo")
public String stepTwo(@Validated(Account.ValidationStepTwo.class) Account account) {...}
Form object:
表单对象:
public class Account {
@NotBlank(groups = {ValidationStepOne.class})
private String username;
@Email(groups = {ValidationStepOne.class})
@NotBlank(groups = {ValidationStepOne.class})
private String email;
@NotBlank(groups = {ValidationStepTwo.class})
@StrongPassword(groups = {ValidationStepTwo.class})
private String password;
@NotBlank(groups = {ValidationStepTwo.class})
private String confirmedPassword;
}
回答by Zelin Zhu
besides above, you can only apply @Valid
on a domain/field for nested validation, not with a @validated
.
除此之外,您只能@Valid
在域/字段上申请嵌套验证,而不能使用@validated
.
回答by Lebecca
In the example code snippets of the question, @Valid
and @Validated
make no difference. But if the @RequestBody
is annotated with a List
object, or is a string value annotated by @RequestParam
, the validation will not take effect.
在问题的示例代码片段中,@Valid
并@Validated
没有什么区别。但是如果@RequestBody
用List
对象注释,或者是用 注释的字符串值@RequestParam
,则验证不会生效。
We can use the @Validated
's method-level validation capability to make it work. To achieve this, the key point is to place @Validated
on the class. This may be another important difference between @Valid
and @Validated
in spring framework.
我们可以使用@Validated
的方法级验证功能使其工作。要做到这一点,关键是@Validated
放在班级上。这可能是spring 框架之间@Valid
和@Validated
中的另一个重要区别。
Refrence
参考