Here is my controller:
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Slf4j
@RestController
@RequestMapping("/auth-api")
public class AuthController {
@PostMapping("/register")
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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
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=""
you need to add @Validated
annotation on your controller, which marks it to be validated.
@Slf4j
@Validated
@RestController
@RequestMapping("/auth-api")
public class AuthController {
// your code
}
Additionally, you have to make sure you have one validator implementation like hibernate-validator
in your classpath.
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
If not already present, you can add above dependency in your pom.xml or any other suitable version from here