Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-pro· GoogleHugging Face LMArena Human Preference 140K

2025-05-29T17:42:51.395+08:00 ERROR 27112 --- [nio-8080-exec-6]…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-sonnet-4-20250514’s answer.
  1. 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

  2. Gemini 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/JSONObject tells you a very specific story:

    1. Compilation was successful: Your code was successfully compiled. At that time, the Java compiler found the com.alibaba.fastjson2.JSONObject class, so it knew what methods and properties were available.
    2. Runtime failed: When your application was actually running and processing a request (Servlet.service()), it tried to create or use an object of the JSONObject class.
    3. Class Not Found: At that exact moment, the Java Virtual Machine (JVM) looked for the JSONObject.class file in the application's classpath (all the JARs and class files available to it) and could not find it.

    In short: The fastjson2 library 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 fastjson2 Dependency (Most Common Fix)

    You are using a class from the fastjson2 library, but you haven't declared it as a dependency in your build file (pom.xml for Maven or build.gradle for Gradle).

    For Maven (pom.xml)

    Add the following dependency to the <dependencies> section of your pom.xml file. 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.gradle or build.gradle.kts)

    Add the following to the dependencies block of your build.gradle file.

    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 provided or test means the dependency will not be included in the final application package.

    For Maven (pom.xml)

    Ensure the dependency does not have a scope of provided or test. It should use the default compile scope.

    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 implementation or api instead of testImplementation or compileOnly.

    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 .m2 repository 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 install
    

    For Gradle

    Run this command from your project's root directory:

    ./gradlew clean build
    

    After running the command, refresh your project in your IDE (e.g., in IntelliJ: File -> Invalidate Caches / Restart... or right-click the pom.xml -> Maven -> Reload Project).


    Solution 4: Check for Dependency Conflicts

    Another library in your project might be pulling in a different, conflicting version of fastjson or excluding it altogether. You can check your project's dependency tree to find out.

    For Maven

    mvn dependency:tree
    

    For Gradle

    ./gradlew dependencies
    

    Look 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 your pom.xml to 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 fastjson2 dependency separately.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.