[英]Annotations from javax.validation.constraints not working (ignored)
I'm trying to use javax constraints to pre-validate a request content before running the logic. 我正在尝试使用javax约束在运行逻辑之前预先验证请求内容。
I have tried any possible solution out there but still can't put javax annotations to work in a Spring boot. 我已经尝试了所有可能的解决方案,但仍然无法在Spring Boot中使用javax注释。
import javax.validation.constraints.Min
import javax.validation.constraints.Pattern
data class LoginRequest (
@Credential //Custom constraint that works just fine
val credential: String,
@Min(value= 5)
val password: String,
@Pattern(regexp = Constants.Regex.DEVICE_ID_REGEX, message = "Invalid device ID")
val device: String
): Serializable
This is a part of pom.xml 这是pom.xml的一部分
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
.
.
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.17.Final</version>
</dependency>
And this is the controller 这是控制器
import javax.validation.Valid
@PostMapping("/login")
fun userLoginEndpoint(@Valid @RequestBody loginRequest: LoginRequest): ResponseEntity<User> {
return authService.loginUser(loginRequest)
?.let{ ResponseEntity(it, HttpStatus.ACCEPTED)}
?: ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()
}
Am I doing something wrong? 难道我做错了什么?
From @JBNizet comment 来自@JBNizet的评论
Replace @Min(value= 5)
(and the other validation annotations too, of course) by @field:Min(value= 5)
. 将
@Min(value= 5)
(当然还有其他验证注释) @field:Min(value= 5)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.