SpringBoot 2.0.0.RELEASE中ErrorAttributes导入无法解析的编译问题
Hey there, I’ve dealt with this exact snag when upgrading to Spring Boot 2.0—let’s walk through what’s causing it and how to fix it quickly.
1. The Root Cause: Package Restructuring
Spring Boot 2.0 completely refactored the web auto-configuration package structure to better separate Servlet and Reactive web stack concerns. The ErrorAttributes class you’re trying to import was moved out of the old org.springframework.boot.autoconfigure.web package.
Correct Import Statement
Depending on which web stack you’re using:
- For Servlet-based applications (most standard Spring Boot web apps), update your import to:
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorAttributes; - For WebFlux (Reactive) applications, use this instead:
import org.springframework.boot.autoconfigure.web.reactive.error.ErrorAttributes;
2. Verify Your Dependencies
Make sure you have the correct Spring Boot starter included in your build file—without it, the ErrorAttributes class won’t be available in your classpath.
Maven (pom.xml)
Ensure you’ve added the web starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Gradle (build.gradle)
Add the web starter dependency:
implementation 'org.springframework.boot:spring-boot-starter-web'
(For WebFlux, replace spring-boot-starter-web with spring-boot-starter-webflux)
3. Bonus: Check Custom Implementations
If you’ve created a custom class that implements or extends ErrorAttributes, double-check the method signatures. Spring Boot 2.0 added new default methods to the interface, so you might need to adjust your implementation to avoid compilation issues.
内容的提问来源于stack exchange,提问作者Salvador Borés

