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

Spring Boot打包报错:Error creating Bean with name 'defaultValidator'

问题解决方案:Spring Boot打包时defaultValidator Bean创建失败

问题根源

该错误是由于JSR-380 Bean Validation(即Hibernate Validator)所需的EL表达式依赖缺失或版本不兼容导致的。Spring Boot 2.3.x版本开始,spring-boot-starter-web不再默认包含validation相关依赖,同时Hibernate Validator运行时需要EL API来支持表达式校验(如@AssertTrue的条件判断、消息插值等),如果缺少对应依赖,就会抛出javax.el.ELManager.getExpressionFactory方法不存在的异常。

分步解决方案

1. 确保引入Validation Starter依赖

在pom.xml中添加spring-boot-starter-validation,它会自动管理Hibernate Validator及相关EL依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

2. 手动补充EL API实现(若步骤1无效)

如果项目中存在依赖冲突导致EL依赖被排除,可手动添加兼容的EL API及实现:

<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.el</artifactId>
    <version>3.0.0</version>
</dependency>

3. 排查并排除冲突依赖

检查pom.xml中是否有其他依赖(如某些旧版容器、自定义组件)排除了javax.el相关包,可通过以下命令分析依赖树:

mvn dependency:tree | grep javax.el

若发现冲突,在对应依赖中添加排除规则,例如:

<dependency>
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

4. 强制更新依赖并重新打包

执行以下命令清理本地缓存并重新构建:

mvn clean install -U

补充检查点

  • 确认application.properties中未禁用Validation自动配置(如spring.autoconfigure.exclude包含ValidationAutoConfiguration)
  • 实体类中的校验注解(如@NotNull、@Size)是否正确导入javax.validation.constraints下的类

内容的提问来源于stack exchange,提问作者Bubu Bhat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 11:15:35