在@RestController 上没有@Validated 时@Valid 可以工作吗?

Does @Valid work without @Validated on @RestController?

在下面的博客条目中。

https://www.baeldung.com/spring-boot-bean-validation

作者提到 Spring Boot 如何与 @Valid 注释一起工作。

@RestController
public class UserController {

    @PostMapping("/users")
    ResponseEntity<String> addUser(@Valid @RequestBody User user) {
        // persisting the user
        return ResponseEntity.ok("User is valid");
    }
    
    // standard constructors / other methods
    
}

When Spring Boot finds an argument annotated with @Valid, it automatically bootstraps the default JSR 380 implementation — Hibernate Validator — and validates the argument.

@Valid 在没有 @Validated 的情况下在 @RestController 上按预期工作是真的吗?

那什么样的立体类型需要显式标注@Validated

@Valid@RestController 中没有 @Validated 也可以工作。

In Spring, we use JSR-303's @Valid annotation for method level validation. Moreover, we also use it to mark a member attribute for validation. However, this annotation doesn't support group validation. Groups help to limit the constraints applied during validation. One particular use case is UI wizards. Here, in the first step, we may have a certain sub-group of fields. In the subsequent step, there may be another group belonging to the same bean. Hence we need to apply constraints on these limited fields in each step, but @Valid doesn't support this. In this case, for group-level, we have to use Spring's @Validated, which is a variant of this JSR-303's @Valid. This is used at the method-level. And for marking member attributes, we continue to use the @Valid annotation.

您可以在 link 中阅读更多相关信息。