You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot测试出现NoClassDefFoundError: FormContentFilter缺失如何解决?

问题原因分析

问题场景

在多模块Maven的Spring Boot 2.1.3.RELEASE项目中,运行以下测试类时测试失败:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UServiceImplTest {

    @Autowired
    UService uService;

    @Test
    public void getMunicipalZips() throws Exception {
        Set<MunicipalityDto> res = uService.getMunicipalZips("FR");
        // ... 测试逻辑
    }
    // ... 其他代码
}

报错信息

2023-09-11 13:25:51.589 ERROR 54426 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Error processing condition on org.springframework.boot.actuate.autoconfigure.audit.AuditAutoConfiguration$AuditEventRepositoryConfiguration
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64) ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
...
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@ed17bee]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:686) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
...
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/filter/FormContentFilter
    at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
...

测试用Spring Boot应用类

测试用的启动类定义在src/test/com.root.package下:

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(TestApplication.class, args);
    }
}

项目pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bla.bla</groupId>
    <artifactId>my-service</artifactId>
    <name>my-service</name>
    <description>Service layer for XXX</description>
    <version>4.6.32-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>11</java.version>
        <spring-statemachine.version>2.0.1.RELEASE</spring-statemachine.version>
    </properties>

    <build>
        <testResources>
            <testResource>
                <directory>${basedir}/src/test/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/*.zip</exclude>
                </excludes>
            </testResource>
            <testResource>
                <directory>${basedir}/src/test/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.zip</include>
                </includes>
            </testResource>
        </testResources>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.bla.bla</groupId>
            <artifactId>my-domain</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.bla.bla</groupId>
            <artifactId>my-soap</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.2.1.RELEASE</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.29</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.statemachine</groupId>
                <artifactId>spring-statemachine-bom</artifactId>
                <version>${spring-statemachine.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

问题原因

  1. Spring Web版本不兼容:Spring Boot 2.1.3.RELEASE默认依赖Spring框架5.1.5.RELEASE,但你手动指定了spring-web为5.0.7.RELEASE。FormContentFilter是Spring 5.1.x版本新增的类,5.0.7中不存在该类,直接导致NoClassDefFoundError。
  2. Spring Security版本不匹配:Spring Boot 2.1.3.RELEASE对应的Spring Security版本应为5.1.4.RELEASE,你手动指定了更高的5.2.1.RELEASE,高版本Security依赖的Spring核心组件版本与当前Boot版本不兼容,进一步引发类加载异常。
  3. 依赖管理混乱:Spring Boot的parent pom已经统一管理了所有Spring生态组件的版本,手动指定组件版本会破坏这种版本一致性,引发依赖冲突或类缺失问题。

内容的提问来源于stack exchange,提问作者belgoros

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:15:53