我正在使用最新的稳定版本的 springboot 和 spring-validator,但是 bean 验证根本没有被调用。 下面是我的 gradle.kts、Bean 和控制器:
plugins {
java
id("org.springframework.boot") version "3.2.2"
id("io.spring.dependency-management") version "1.1.4"
id("org.jetbrains.kotlin.plugin.spring") version "1.9.22"
kotlin("plugin.noarg") version "1.9.22"
kotlin("plugin.jpa") version "1.9.22"
kotlin("jvm")
}
group = "com.study.alura.challenge"
version = "0.0.1-SNAPSHOT"
java {
}
repositories {
mavenCentral()
}
dependencies {
// Spring
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-log4j2")
developmentOnly("org.springframework.boot:spring-boot-devtools")
implementation("org.springframework.boot:spring-boot-starter-validation:3.2.2")
//Database
implementation("org.flywaydb:flyway-core")
runtimeOnly("org.postgresql:postgresql")
//AWS
implementation("com.amazonaws:aws-java-sdk:1.12.644")
//Kotlin
runtimeOnly("org.jetbrains.kotlin:kotlin-reflect:1.9.22")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.16.1")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.1")
//Testing
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation(kotlin("stdlib-jdk8"))
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:2023.0.0")
}
}
configurations {
all {
exclude("org.springframework.boot", module = "spring-boot-starter-logging")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
package com.study.alura.challenge.journeymiles.user.controller
import com.study.alura.challenge.journeymiles.user.dto.request.CreateUserRequestDTO
import com.study.alura.challenge.journeymiles.user.dto.response.CreateUserResponseDTO
import com.study.alura.challenge.journeymiles.user.service.UserService
import jakarta.validation.Valid
import org.apache.logging.log4j.LogManager
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/users")
class UserController(private val userService: UserService) {
private val logger = LogManager.getLogger(this::class.java)
@PostMapping
fun create(@RequestBody @Valid createUserRequestDTO: CreateUserRequestDTO): ResponseEntity<CreateUserResponseDTO> {
return ResponseEntity.ok(
userService.createUser(createUserRequestDTO).also {
logger.info("Created a new user with id: ${it.id}")
}
)
}
}
package com.study.alura.challenge.journeymiles.user.dto.request
import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import java.io.Serializable
data class CreateUserRequestDTO(
@NotNull(message = "The name field is mandatory")
@Size(min = 3, max = 100, message = "The name field must bet between 3 and 100 chars")
val name: String,
@NotNull(message = "The email field is mandatory")
@Email
val email: String,
@NotNull(message = "The password field is mandatory")
@Size(min = 6, max = 12, message = "The password field must bet between 6 and 12 chars")
val password: String
): Serializable
有人对缺少的内容有任何提示吗?
我几乎遵循了互联网上的所有教程,但仍然找不到代码有什么问题
使用bean验证,并触发验证
您可能将 Java 验证与 Kotlin 的验证混合在一起。尝试一下
data class CreateUserRequestDTO(
@field:NotNull(message = "The name field is mandatory")
@field:Size(min = 3, max = 100, message = "The name field must bet between 3 and 100 chars")
val name: String,
@field:NotNull(message = "The email field is mandatory")
@field:Email
val email: String,
@field:NotNull(message = "The password field is mandatory")
@field:Size(min = 6, max = 12, message = "The password field must bet between 6 and 12 chars")
val password: String
): Serializable
您还可以查看 https://www.baeldung.com/kotlin/valid-spring-annotation