Skip to content

Built-in support for standard bean validation on controller method arguments  #24913

Closed as not planned
@hedgerocker

Description

@hedgerocker

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.

Metadata

Metadata

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by anothertype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions