调用API时出现Handler dispatch failed及NoClassDefFoundError错误求助
Alright, let's break down this error you're encountering:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
The core issue here is the java.lang.NoClassDefFoundError — this means your application can't locate the org.apache.http.client.HttpClient class when it's running. The outer NestedServletException is just Spring MVC wrapping this underlying problem while trying to route a request.
Common Causes
- Missing Apache HttpClient Dependency: The most frequent reason — your project doesn't include the required Apache HttpClient library in its runtime classpath.
- Dependency Version Conflict: Multiple versions of HttpClient (or related components) exist in your project, causing conflicts that prevent the class from loading correctly.
- Incorrect Dependency Scope: If you marked the HttpClient dependency as
provided(Maven) orcompileOnly(Gradle), it won't be included in the runtime classpath unless your deployment environment explicitly supplies it.
Fixes & Solutions
1. Add the Correct Apache HttpClient Dependency
Pick the dependency that matches the version your code uses:
For Apache HttpClient 4.x (most common for older projects)
Maven:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.14</version> <!-- Use the latest stable 4.x version --> </dependency>
Gradle:
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
For Apache HttpClient 5.x (newer version with updated packages)
Note: In 5.x, the HttpClient class lives under org.apache.hc.client5.http.classic instead. If your code uses 5.x APIs, use this dependency:
Maven:
<dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.2.1</version> <!-- Use the latest stable 5.x version --> </dependency>
Gradle:
implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
2. Resolve Version Conflicts
If you suspect a dependency conflict, run these commands to inspect your project's dependency tree:
- Maven:
mvn dependency:tree - Gradle:
gradle dependencies
Look for duplicate or conflicting HttpClient entries, then exclude the unwanted version from the problematic dependency. For example, in Maven:
<dependency> <groupId>com.example.some-dependency</groupId> <artifactId>some-artifact</artifactId> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> </exclusions> </dependency>
3. Fix Dependency Scope
If you had set the HttpClient dependency to provided or compileOnly, switch it to implementation (for modern Maven/Gradle) or compile (for older Maven versions) to ensure it's included in the runtime classpath.
Final Step
After adjusting dependencies, clean your project (run mvn clean or gradle clean) and rebuild. Restart your application, and the class-not-found error should be resolved.
内容的提问来源于stack exchange,提问作者Rajesh Bhushan

