我正在處理一個 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 的一些變體,但沒有任何效果。
我希望有人看到我的錯誤。非常感謝!
uj5u.com熱心網友回復:
示例:推薦data class @field:NotBlank而不需要@Validated
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395728.html
上一篇:RecylerView資料未顯示
