'Spring Validating requests params

I want to add validation annotations to my requests params in a Spring annotation. I have the required javax dependencies and code looks something like this

@RestController
public class Controller {

    @RequestMapping(value = "/api", method = RequestMethod.GET)
    public ExternalUserStatusCollection getUser(
            @RequestParam(value = "userId", required = false) @Validated @Size(min = 5) @NotNull UserId userId,
 {.....
}

However when I run the application, it doesnt validate the required fields. I dont want to modify the DTO of the object since the intention is to validate some DTO fields coming from external dependencies as well. How can I achieve this using annotations? Annotating controller with @Validated leads to a bunch of other errors & doesnt help either



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source