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);
}