Getting NoSuchMethodError javax.servlet.ServletContext.addServlet in Spring Boot while running a Spring MVC application
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NoSuchMethodError: javax.servlet.ServletContext.addServlet in Spring Boot means there is a servlet API version conflict on the classpath. Spring Boot's embedded Tomcat requires Servlet API 3.1+ (for Spring Boot 2.x) or Jakarta Servlet 6.0+ (for Spring Boot 3.x). This error occurs when an older servlet API jar (2.5 or 3.0) is pulled in as a transitive dependency and takes precedence over the correct version. The fix is to exclude the conflicting dependency.
The Error
Or in Spring Boot 3.x:
The addServlet method was added in Servlet 3.0. If an older servlet API is loaded first, the method does not exist.
Fix 1: Find and Exclude the Conflicting Dependency
Maven
Exclude it:
Gradle
Fix 2: Ensure the Correct Servlet API Version
For Spring Boot 2.x:
For Spring Boot 3.x (Jakarta EE 9+):
Fix 3: Set the Correct scope for servlet-api
If you must declare the servlet API explicitly, mark it as provided:
provided scope means the dependency is available at compile time but not packaged — the embedded Tomcat provides it at runtime.
Diagnosing the Classpath Conflict
Spring Boot 3.x Migration (javax to jakarta)
Spring Boot 3.x moved from javax.servlet to jakarta.servlet:
If you see the error after upgrading to Spring Boot 3.x, check for leftover javax.servlet imports and dependencies:
Common Pitfalls
- Manually adding
servlet-apito the POM: Spring Boot'sspring-boot-starter-webincludes the correct servlet API viatomcat-embed-core. Manually addingjavax.servlet:servlet-apicreates a conflict. Remove the explicit dependency and let Spring Boot manage it. - Legacy libraries bundling old servlet API: Libraries written for older Java EE versions often declare
javax.servlet:servlet-api:2.5as a compile dependency. Always checkmvn dependency:treeand exclude these transitive dependencies. - Running on an external Tomcat with wrong version: If deploying to an external Tomcat instead of the embedded one, the Tomcat version must match Spring Boot's requirements. Spring Boot 2.x needs Tomcat 9+; Spring Boot 3.x needs Tomcat 10+.
- Mixing Spring Boot 3.x with javax dependencies: Spring Boot 3.x uses Jakarta EE 9+ (jakarta namespace). Any library that depends on
javax.servletcauses conflicts. Either upgrade the library or use Spring Boot 2.x. - IDE caching stale classpath: After fixing the POM, IDEs may still use cached jars. Run
mvn cleanand reimport the project in your IDE (Maven > Reload Project in IntelliJ).
Summary
- The error means an old servlet API jar (2.5 or 3.0) is on the classpath
- Find the conflict with
mvn dependency:tree -Dincludes=javax.servlet - Exclude the old
servlet-apifrom the offending transitive dependency - Do not manually add servlet API dependencies — Spring Boot's starter includes the correct version
- Spring Boot 3.x uses
jakarta.servlet, notjavax.servlet— update all imports after migration - After fixing the POM, clean the build and reimport in your IDE

