Spring Boot版本升级后javax.servlet.http.HttpServletRequest无法解析及Spring Boot核心类导入失败问题求助
Hey there, let's break down what's happening here and fix those import errors step by step.
Why You're Seeing These Issues
The root cause here is a dependency conflict. Spring Boot's spring-boot-starter-web already includes the javax.servlet-api dependency out of the box (with provided scope, since your servlet container like Tomcat will provide it at runtime). When you manually added the same dependency to your pom.xml, you created a version mismatch that messed up Spring Boot's internal dependency management—this is why your core Spring classes suddenly became unimportable.
Step-by-Step Solutions
Remove the manually added
javax.servlet-apidependency
Go into yourpom.xmland delete this entire block:<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency>Spring Boot's starter-web already handles this for you, so you don't need to declare it separately.
Verify your Spring Boot parent dependency is correctly set
Make sure yourpom.xmlhas the Spring Boot starter parent as the parent project—this ensures all your Spring Boot dependencies use consistent, compatible versions. Here's what it should look like (adjust the version to match what you're using, 2.5.12 or 2.6.6):<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- Lets Maven pull the parent from the repo --> </parent>If you can't use the parent (e.g., you have a custom parent), use a
dependencyManagementblock to import Spring Boot's dependency bill of materials instead:<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.6.6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Clean and rebuild your project
Run this Maven command to clear cached dependencies and rebuild from scratch:mvn clean installIf you're using an IDE like IntelliJ or Eclipse, also refresh your Maven/Gradle project (in IntelliJ, right-click your project → Maven → Reload All Projects) to ensure the IDE picks up the corrected dependencies.
Check for leftover dependency issues
If you still see errors, use Maven's dependency tree to spot other conflicts:mvn dependency:treeLook for any entries where Spring Boot core dependencies (like
spring-boot,spring-boot-autoconfigure) are marked as excluded or have mismatched versions.
Why This Works
Spring Boot's starter dependencies are designed to handle all transitive dependencies for you—including the servlet API. By removing your manual dependency and ensuring consistent version management via the starter parent or BOM, you eliminate the conflict that was blocking access to Spring's core classes.
内容的提问来源于stack exchange,提问作者KL Ray

