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

Spring Boot从低版本升级至2.5.5后@Valid注解出现编译错误如何解决

问题根因

Spring Boot 自2.3版本起,spring-boot-starter-web 移除了对 spring-boot-starter-validation 的默认集成。你在2.1.7.RELEASE版本中可正常使用的@Valid注解、javax.validation相关类,均来自旧版本web starter自带的间接依赖,升级到2.5.5版本后该依赖缺失,因此触发编译错误。

解决方案

直接手动引入validation starter依赖即可修复问题,根据你使用的项目构建工具选择对应配置:

  • Maven项目(pom.xml)添加依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
  • Gradle项目(build.gradle)添加依赖:
implementation 'org.springframework.boot:spring-boot-starter-validation'

添加完成后刷新项目依赖即可正常编译,Spring Boot 2.5.5版本的validation starter完全兼容原有javax.validation包的导入语句,无需修改现有代码中的注解及导入路径。

内容的提问来源于stack exchange,提问作者Ravindra Pandya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 10:09:03