With the following validation setup in an annotated MVC controller:
@RestController
@RequestMapping("/users")
@Validated // <-- without this, the @Size annotation in setPassword() has no effect
public class UserController {
@PutMapping("/{id}/password")
public void setPassword(@PathVariable long id, @RequestBody @Size(min = 8) String password) {
/* ... */
}
@PutMapping("/{id}/other")
public void setOther(@PathVariable long id, @RequestBody @Valid MyFormObject form) {
/* ... */
}
}
@Validated on the controller is required for the method parameter since it's not a "complex" object. In comparison, the @Valid annotation on the setOther method works without the @Validated annotation.
Why is @Validated required? Why not enable it by default? Is there a cost to its use?
edit
Note that Difference between @Valid and @Validated in Spring is related (I read it before asking this), but it doesn't address the why in my question.