2025-05-29T17:42:51.395+08:00 ERROR 27112 --- [nio-8080-exec-6]…
- User
2025-05-29T17:42:51.395+08:00 ERROR 27112 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed: java.lang.NoClassDefFoundError: com/alibaba/fastjson2/JSONObject] with root cause
java.lang.ClassNotFoundException: com.alibaba.fastjson2.JSONObject
Assistant
Of course. This is a very common error in Java applications, especially in the Spring ecosystem. Let's break down what this error means and how to fix it.
What the Error Means
The error
java.lang.NoClassDefFoundError: com/alibaba/fastjson2/JSONObjecttells you a very specific story:- Compilation was successful: Your code was successfully compiled. At that time, the Java compiler found the
com.alibaba.fastjson2.JSONObjectclass, so it knew what methods and properties were available. - Runtime failed: When your application was actually running and processing a request (
Servlet.service()), it tried to create or use an object of theJSONObjectclass. - Class Not Found: At that exact moment, the Java Virtual Machine (JVM) looked for the
JSONObject.classfile in the application's classpath (all the JARs and class files available to it) and could not find it.
In short: The
fastjson2library was available when you built the project, but it's missing from the final packaged application (the JAR/WAR file) that is being executed.This is a classic classpath issue.
Common Causes and How to Fix Them
Here are the most likely reasons for this error and the steps to resolve it.
Solution 1: Add the
fastjson2Dependency (Most Common Fix)You are using a class from the
fastjson2library, but you haven't declared it as a dependency in your build file (pom.xmlfor Maven orbuild.gradlefor Gradle).For Maven (
pom.xml)Add the following dependency to the
<dependencies>section of yourpom.xmlfile. It's good practice to check Maven Central for the latest version.<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson2</artifactId> <version>2.0.47</version> <!-- Or the latest version --> </dependency>For Gradle (
build.gradleorbuild.gradle.kts)Add the following to the
dependenciesblock of yourbuild.gradlefile.Groovy DSL (
build.gradle):dependencies { implementation 'com.alibaba:fastjson2:2.0.47' // Or the latest version }Kotlin DSL (
build.gradle.kts):dependencies { implementation("com.alibaba:fastjson2:2.0.47") // Or the latest version }
Solution 2: Check the Dependency Scope
Sometimes the dependency is present but has the wrong "scope". A scope like
providedortestmeans the dependency will not be included in the final application package.For Maven (
pom.xml)Ensure the dependency does not have a scope of
providedortest. It should use the defaultcompilescope.Correct:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson2</artifactId> <version>2.0.47</version> <!-- <scope>compile</scope> is the default, so it can be omitted --> </dependency>Incorrect (will cause the error):
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson2</artifactId> <version>2.0.47</version> <scope>provided</scope> <!-- WRONG! --> </dependency>For Gradle (
build.gradle)Ensure you are using
implementationorapiinstead oftestImplementationorcompileOnly.Correct:
implementation 'com.alibaba:fastjson2:2.0.47'Incorrect (will cause the error):
// This is only for tests testImplementation 'com.alibaba:fastjson2:2.0.47' // This is only for compile time, not included at runtime compileOnly 'com.alibaba:fastjson2:2.0.47'
Solution 3: Clean and Rebuild Your Project
Your local build cache (Maven's
.m2repository or Gradle's cache) might be corrupted, or your IDE might be out of sync. A clean rebuild often fixes this.For Maven
Run this command from your project's root directory:
mvn clean installFor Gradle
Run this command from your project's root directory:
./gradlew clean buildAfter running the command, refresh your project in your IDE (e.g., in IntelliJ:
File -> Invalidate Caches / Restart...or right-click thepom.xml->Maven -> Reload Project).
Solution 4: Check for Dependency Conflicts
Another library in your project might be pulling in a different, conflicting version of
fastjsonor excluding it altogether. You can check your project's dependency tree to find out.For Maven
mvn dependency:treeFor Gradle
./gradlew dependenciesLook through the output for
fastjson2. If you see it's being managed or excluded by another dependency, you may need to use a<dependencyManagement>section in yourpom.xmlto enforce the version you want.
Important Note for Spring Boot Users
By default, Spring Boot uses Jackson for JSON serialization/deserialization. If your goal is to replace Jackson with Fastjson2 for handling HTTP requests and responses (
@RequestBody,@ResponseBody), you should use the official starter.This starter correctly configures Fastjson2 as the primary JSON mapper and excludes Jackson.
For Maven (
pom.xml)Add the
fastjson2-spring-boot-starter:<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-spring-boot-starter</artifactId> <version>2.0.47</version> <!-- Or the latest version --> </dependency>Using this starter is the recommended way to integrate Fastjson2 into a Spring Boot application. If you add this, you don't need to add the plain
fastjson2dependency separately.- Compilation was successful: Your code was successfully compiled. At that time, the Java compiler found the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.