Closed as not planned
Closed as not planned
Description
Related to #11041.
I'm trying to validate @PathVariable
and @RequestBody
in one method. MethodValidationPostProcessor
is registered. Simplified Controller looks like this:
@RestController
@Validated
public class MyController {
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity method_name(@PathVariable @NotNull String id, @Valid @RequestBody Body body) {
/// Some code
}
}
I noticed that request body validation is triggered twice - it wouldn't be an issue if it weren't custom ConstraintValidator
with injected bean:
public class CustomValidator implements ConstraintValidator<Body, String> {
@Autowired
private BodyVerificator bodyVerificator;
...
For first validation run the bean is injected properly, and for second run it's null so it results in NPE. Workaround is to either not use method parameter validation at all, or to annotate @RequestBody
with @Validated
so not mix @Validated
and @Valid
. I would actually prefer to have @PathVariable
bean validation support in Spring like in #11041.