+-
@Valid vs @Validated in Spring Boot Rest API @RequestBody [复制]
参见英文答案 > Difference between @Valid and @Validated in Spring                                    2个
我来自.NET背景开发REST API.现在使用spring boot处理java REST项目.

首先,我在控制器@RequestBody的验证才停止工作.在尝试修复它时,我看到了不同的实现方法.那么注释@RequestBody的正确方法是什么?

@Validated @RequestBody

要么

@Valid @RequestBody
最佳答案
两者之间通常没有太大差异,@ Valid来自JSR-303标准,@ Validated是弹簧标准.根据春季文件:

Spring provides a Validator interface that can be used for validation in all layers of an application. In Spring MVC you can configure it for use as a global Validator instance, to be used whenever an @Valid or @Validated controller method argument is encountered, and/or as a local Validator within a controller through an @InitBinder method. Global and local validator instances can be combined to provide composite validation.

参考:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-validation

但是有一些区别,例如,如果您需要在验证注释中使用group,则需要使用@Validated,而@Valid不支持它.

点击查看更多相关文章

转载注明原文:@Valid vs @Validated in Spring Boot Rest API @RequestBody [复制] - 乐贴网