【发布时间】:2021-12-27 10:20:35
【问题描述】:
我正在开发一个 Spring Boot 项目,目前正在尝试实施验证。例如,我有以下课程:
包 abcdef.mypackage
import java.util.*
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.validation.constraints.Email
import javax.validation.constraints.NotBlank
@Entity
class User (
@Id
@GeneratedValue
var id: Long,
@Column(name="username", unique = true, nullable = false)
@NotBlank
var username: String,
@Column(name="password", unique = false, nullable = false)
var password: String,
@Column(name="firstname", unique = false, nullable = false)
@NotBlank
var firstname: String,
@Column(name="lastname", unique = false, nullable = false)
@NotBlank
var lastname: String,
@Column(name = "birthdate", unique = false, nullable = true)
var birthdate: Date? = null,
@Column(name="email", unique = true, nullable = false)
@Email
var email: String,
@Column(name="phone", unique = true, nullable = false)
var phone: String,
)
你可以看到,我已经用我想要的验证注释了所有字段。传入请求由以下控制器类处理:
package abcdef.mypackage
import org.springframework.http.ResponseEntity
import org.springframework.validation.BindingResult
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import javax.validation.Valid
@RestController
@RequestMapping("/api/v1/users/")
@Validated
class UserResource(val service: UserService) {
@PostMapping("/register")
@Validated
fun post(@Valid @RequestBody user: User, result: BindingResult) : ResponseEntity<Unit> {
if (result.hasErrors()) {
return ResponseEntity.badRequest().build()
}
try {
service.post(user)
} catch (e: Exception) {
return ResponseEntity.badRequest().build()
}
return ResponseEntity.ok().build()
}
}
当我现在使用例如空白用户名值发出请求时,Spring Boot 仍然接受它并将其存储到数据库中。我在 StackOverflow 上找到了一些关于缺少依赖项的问题(和答案),但我包括了所有这些。您可以在这里查看我的依赖项:
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
我不知道该怎么做才能使此验证正常工作...我在依赖项中看不到任何问题。我还尝试了一些使用 @Valid 和 @Validated 的变体,但对我没有任何效果。
我希望有人看到我的错误。非常感谢!
【问题讨论】:
标签: spring spring-boot kotlin validation