Published 17 Oct, 2022

Java - javax.validation.constraints does not work with Spring web

Category Java
Modified : Nov 28, 2022

Here is my controller:

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

public class AuthController {

    public RequestResultJSON<String> register(@RequestParam @NotEmpty String username,
                                              @RequestParam @NotEmpty String password,
                                              @RequestParam @NotEmpty  String passwordConfirm);

And i use this dependencies in my pom.xml file




When I make a post request I expect that method execution will not happen, but it happens with incorrect parameters

Request: http://localhost:8080/auth-api/register?username&password&passwordConfirm

And register method execute with username="", password="", passwordConfirm=""


There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Rest, Spring, Javax.validation. These have been categorized in sections for a clear and precise explanation.


you need to add @Validated annotation on your controller, which marks it to be validated.

public class AuthController {
  // your code

Additionally, you have to make sure you have one validator implementation like hibernate-validator in your classpath.


If not already present, you can add above dependency in your pom.xml or any other suitable version from here