You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Bean Validator未触发问题排查求助

Spring Boot 3.2.2中Bean Validation完全未触发的排查求助

我使用最新稳定版Spring Boot(3.2.2)及Spring Validator,但Bean Validation完全未触发。以下是我的相关代码配置:

Gradle配置(build.gradle.kts)

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)
}

Controller代码

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}")
            }
        )
    }
}

DTO代码

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

我几乎查阅了网上所有教程仍未找到问题所在,请求提供排查思路。

更新:Sachin的解决方案已生效!


内容的提问来源于stack exchange,提问作者Felipe Barata

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 04:54:56