Spring Boot项目启动报错:配置处理异常与javax类找不到
问题根源与解决方案
你的问题核心是Spring Boot 3.x 全面切换到 Jakarta EE 规范(替代原 Java EE 的 javax 包),同时 pom.xml 存在依赖冲突、冗余依赖和版本不一致的问题,导致类找不到、自动配置失败等异常。
以下是修正后的完整 pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wahch</groupId> <artifactId>todoapp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>todoapp</name> <description>Todo app</description> <properties> <java.version>19.0.2</java.version> </properties> <dependencies> <!-- Spring Boot Starter 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</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-security</artifactId> </dependency> <!-- 工具类依赖 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.11.5</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.11.5</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.11.5</version> <scope>runtime</scope> </dependency> <!-- 测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
关键修改说明:
- 替换javax依赖为Jakarta:移除了
javax.servlet-api,Spring Boot 3.x的spring-boot-starter-web已经内置了jakarta.servlet-api,无需手动引入 - 移除冲突依赖:删除了重复引入的
spring-security-config(同时存在5.5.2和6.2.0版本),Spring Boot Starter Security会自动管理正确的版本 - 删除冗余依赖:
- 移除了单独的
spring-boot-starter(已被spring-boot-starter-web包含) - 移除了
spring-boot-starter-tomcat(spring-boot-starter-web默认包含,且scope无需手动设置) - 移除了手动指定版本的
spring-context、spring-security-core、spring-security-crypto(Spring Boot Parent会统一管理这些依赖的版本,避免冲突) - 移除了
spring-boot-autoconfigure(Parent已包含)
- 移除了单独的
- 规范测试依赖:删除了手动指定版本的
junit-jupiter-api、mockito-core,改用spring-boot-starter-test(已包含这些测试库的兼容版本),新增spring-security-test用于Spring Security相关测试 - 优化JJWT依赖:给
jjwt-impl和jjwt-jackson设置scope=runtime,因为只有运行时才需要实现类
后续操作:
- 执行
mvn clean install清理旧依赖并重新构建 - 直接运行Spring Boot主类即可(社区版IDE也可以右键主类选择Run,无需特殊Spring Boot配置)
内容的提问来源于stack exchange,提问作者Giuseppe
相关产品推荐
相关产品推荐

