我正在尝试使用 javax 约束在运行逻辑之前预先验证请求内容。
我已经尝试了任何可能的解决方案,但仍然无法在 Spring 引导中使用 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
这是 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>
这是控制器
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()
}
难道我做错了什么?
qq_笑_17
相关分类