1

Thanks to (Usage of @Valid vs @Validated in Spring) and Difference between @Valid and @Validated in Spring. I now understand the difference between them.

However, I am unable to understand a particular scenario.

I have an Integer @PathVariable that I receive in a controller endpoint that I'd like to validate using JSR-303 bean validation (I'd only be using @Min(1)) to ensure I get an integer >=1 or validation fails.

Confusion- The setting doesn't work until I put @Validated on controller class, Tried @Valid but that didnt work. Just trying to figure out why is it required ? When I validate a pojo with annotation @Valid I am not forced to use @Validated on class. I saw in Spring docs for @Validated , it quotes

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.

But I am unable to establish relation between @Min and class level @Validated annotation. Any help would be hugely appreciated , also excuse me if I have been unable to raise my question clearly. Any edits or suggestions are welcome.

Below is my example-

@RestController
@Validated
public class PrimeController {

...

@GetMapping("/is-prime/{number}")
    public ObjectNode checkPrime( @Min(1) @PathVariable Integer number) {
        return primeHandler.getPrimalityResult(number);
    }
Saurabh Sharma
  • 489
  • 5
  • 20
  • 1
    `@Valid` on a `@ModelAttribute` or `@RequestBody` is handled by the `DispatcherServlet` (or actually more accurate the Spring MVC `RequestMappingHandlerAdapter` which has this build in). When adding plain javax.validation annotations to method arguments (regardless if this is a controller or not) you must annotate this class with `@Validatied` so that using AOP the `MethodValidationInterceptor` can be applied. They are different mechanism to do validation. – M. Deinum Mar 08 '21 at 07:58
  • Thank you so much @M.Deinum, It's more clearer now. Is there any way to understand this in little more detail ? how exactly spring internally does all this ? Thanks for your comment , i was never gonna be able to establish this relation specially about "MethodValidationInterceptor" myself. – Saurabh Sharma Mar 08 '21 at 11:51
  • 1
    The `RequestMappingHandlerAdapter` checks if a method argument is annotated with `@Valid(ated)` and will call the configured validator. To be more precise this ishandled by the `MethodArgumentResolver` responsible for `@ModelAttribute` or `@RequestBody`. – M. Deinum Mar 08 '21 at 14:51
  • Thanks @M.Deinum, I am happy to accept it as an answer if you put it in answers. – Saurabh Sharma Mar 08 '21 at 16:02

1 Answers1

1

@Valid on a @ModelAttribute or @RequestBody is handled by the DispatcherServlet (or actually more accurate the Spring MVC RequestMappingHandlerAdapter which ultimately delegates this to the ModelAttributeMethodProcessor or RequestResponseBodyMethodProcessor which has this build in).

When adding plain javax.validation annotations to method arguments (regardless if this is a controller or not) you must annotate this class with @Validatied so that using AOP the MethodValidationInterceptor can be applied. They are a different mechanism for applying validation.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224