@Validated is used to declare that arguments should be validated. It'd be great if there was a simple way to declare that method response should be validated too.

In case of Spring MVC, I'd expect such methods to return 500 Internal error.

With easy response validation it'd be easier to declare method and class invariants and valid state specification.

Comment From: quaff

https://github.com/spring-projects/spring-framework/blob/b595dc1dfad9db534ca7b9e8f46bb9926b88ab5a/spring-context/src/main/java/org/springframework/validation/annotation/Validated.java#L51 @Validated can be annotated on ElementType.METHOD not just ElementType.PARAMETER

Comment From: lpandzic

I wasn't clear in original request, I'd like to be able to use @Validated at type level and get same behavior for response validation same like I'd get for argument validation.

Comment From: vishalsingh2972

@lpandzic @rstoyanchev can you assign this to me if it's still open ?

Comment From: rstoyanchev

@vishalsingh2972, generally we only assign issues to team members, and typically you would need to submit a pull request with changes to be reviewed anyway. However, this issue is not marked ideal for contribution, nor fully triaged as evident from the issue labels.

Comment From: rstoyanchev

@lpandzic, from what I can see @Validated does validate the return value and that has been in place for a long time. Have you tried it and found that it doesn't work?

Comment From: vishalsingh2972

@vishalsingh2972, generally we only assign issues to team members, and typically you would need to submit a pull request with changes to be reviewed anyway. However, this issue is not marked ideal for contribution, nor fully triaged as evident from the issue labels.

Sorry @rstoyanchev new to open source so have no idea on which issues are not ideal for contribution 🤔

Comment From: haitaoss

@lpandzic, from what I can see @Validated does validate the return value and that has been in place for a long time. Have you tried it and found that it doesn't work?

In fact, Spring provides this ability to register MethodValidationPostProcessor into a container and then use @ Validated to mark the bean, enabling verification of method return values

example code

@Configuration
public class SpringValidatorConfig {

    @Bean
    public MethodValidationPostProcessor validationPostProcessor(Validator validation) {
        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setValidator(validation);
        return methodValidationPostProcessor;
    }

}

@Import(SpringValidatorConfig.class)
@Validated
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ValidatedTest {
    @NotNull(message = "{name}不能为null")
    @Value("haitao")
    public String name;

    @NotNull(message = "返回值不能为null")
    public Object test_Validated(@Valid @NotNull ValidatedTest test) {
        System.out.println("test = " + test);
        return null;
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ValidatedTest.class);

        try {
            ValidatedTest bean = context.getBean(ValidatedTest.class);
            bean.test_Validated(new ValidatedTest("haitao"));
        } catch (ConstraintViolationException e) {
        }
    }
}

Comment From: rstoyanchev

If you're a first time contributor, I would not recommend starting from any issue. We use a label, ideal-for-contribution to mark those issues that should be reasonably straight forward.

Comment From: snicoll

I am going to close this due to the lack of feedback to https://github.com/spring-projects/spring-framework/issues/27162#issuecomment-1511388200. We can reopen if the feedback is provided, but we'll need a bit more details.