使用@Min 进行@PathVariable 验证的@Valid 和@Validated 之间的关系
Relation between @Valid & @Validated for a @PathVariable validation using @Min
感谢(Usage of @Valid vs @Validated in Spring) and 。我现在明白了它们之间的区别了。
但是,我无法理解特定场景。
我有一个整数 @PathVariable
,我在控制器端点接收到它,我想使用 JSR-303
bean 验证(我只使用 @Min(1)
)来验证它确保我得到一个整数 >=1
或验证失败。
Confusion- 在我将 @Validated
放在控制器 class 上之前,该设置不起作用,尝试过 @Valid
但那没有用。只是想弄清楚为什么需要它?
当我验证带有注释@Valid 的 pojo 时,我不会被迫在 class 上使用 @Validated
。
我在 Spring 文档中看到 @Validated
,它引用
Variant of JSR-303's javax.validation.Valid, supporting the specification of validation groups. Designed for convenient use with Spring's JSR-303 support but not JSR-303 specific.
但我无法在 @Min
和 class 级别 @Validated
注释之间建立关系。
任何帮助将不胜感激,如果我无法清楚地提出我的问题,也请原谅。欢迎任何编辑或建议。
下面是我的例子-
@RestController
@Validated
public class PrimeController {
...
@GetMapping("/is-prime/{number}")
public ObjectNode checkPrime( @Min(1) @PathVariable Integer number) {
return primeHandler.getPrimalityResult(number);
}
@ModelAttribute
或 @RequestBody
上的 @Valid
由 DispatcherServlet
处理(或者更准确地说 Spring MVC RequestMappingHandlerAdapter
最终将其委托给具有此内置功能的 ModelAttributeMethodProcessor
or RequestResponseBodyMethodProcessor
。
当向方法参数添加纯 javax.validation
注释时(无论这是否是控制器),您必须使用 @Validatied
注释此 class 以便使用 AOP MethodValidationInterceptor
can be applied。它们是应用验证的不同机制。
感谢(Usage of @Valid vs @Validated in Spring) and
但是,我无法理解特定场景。
我有一个整数 @PathVariable
,我在控制器端点接收到它,我想使用 JSR-303
bean 验证(我只使用 @Min(1)
)来验证它确保我得到一个整数 >=1
或验证失败。
Confusion- 在我将 @Validated
放在控制器 class 上之前,该设置不起作用,尝试过 @Valid
但那没有用。只是想弄清楚为什么需要它?
当我验证带有注释@Valid 的 pojo 时,我不会被迫在 class 上使用 @Validated
。
我在 Spring 文档中看到 @Validated
,它引用
Variant of JSR-303's javax.validation.Valid, supporting the specification of validation groups. Designed for convenient use with Spring's JSR-303 support but not JSR-303 specific.
但我无法在 @Min
和 class 级别 @Validated
注释之间建立关系。
任何帮助将不胜感激,如果我无法清楚地提出我的问题,也请原谅。欢迎任何编辑或建议。
下面是我的例子-
@RestController
@Validated
public class PrimeController {
...
@GetMapping("/is-prime/{number}")
public ObjectNode checkPrime( @Min(1) @PathVariable Integer number) {
return primeHandler.getPrimalityResult(number);
}
@ModelAttribute
或 @RequestBody
上的 @Valid
由 DispatcherServlet
处理(或者更准确地说 Spring MVC RequestMappingHandlerAdapter
最终将其委托给具有此内置功能的 ModelAttributeMethodProcessor
or RequestResponseBodyMethodProcessor
。
当向方法参数添加纯 javax.validation
注释时(无论这是否是控制器),您必须使用 @Validatied
注释此 class 以便使用 AOP MethodValidationInterceptor
can be applied。它们是应用验证的不同机制。