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

添加spring-boot-starter-validation依赖后Spring Boot项目启动失败求助

问题排查:添加spring-boot-starter-validation后Spring Boot启动失败(找不到主类)

问题描述

我正在开发一个基于MongoDB的Spring Boot小型项目,想要为Player实体类添加校验功能。但在pom.xml中添加spring-boot-starter-validation依赖后,启动项目时出现错误:找不到或无法加载主类com.classhacker.tttapi.DemoApplication,移除该依赖则项目可正常启动。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.classhacker</groupId>
    <artifactId>tttapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tttapi</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Player实体类代码

package com.classhacker.tttapi.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Player {
    
    @Id
    int _id;
    
    String name;
    
    int gamesWon;

    public Player(int _id, String name, int gamesWon) {
        super();
        this._id = _id;
        this.name = name;
        this.gamesWon = gamesWon;
        }
    
    public int get_id() {
        return _id;
    }
    
    public String getName() {
        return name;
    }
    
    public int getGamesWon() {
        return gamesWon;
    }
    
    @Override
    public String toString() {
        return String.format("Player[id=%d, name=%s, gamesWon=%d]", _id, name, gamesWon);
    }
}

报错信息

Error: Could not find or load main class com.classhacker.tttapi.DemoApplication
Caused by: java.lang.ClassNotFoundException: com.classhacker.tttapi.DemoApplication

排查与解决方案

  • 清理Maven依赖缓存:执行命令mvn clean install -U,强制更新依赖并重新构建项目,解决可能的依赖缓存或冲突问题。
  • 确认主类正确性:检查DemoApplication类是否在com.classhacker.tttapi包下,类上是否标注@SpringBootApplication注解,类名拼写(包括大小写)是否完全匹配报错信息中的路径。
  • 显式指定主类:在spring-boot-maven-plugin中添加主类配置,确保插件能正确识别启动类:
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>com.classhacker.tttapi.DemoApplication</mainClass>
        </configuration>
    </plugin>
    
  • 检查依赖冲突:执行mvn dependency:tree查看依赖树,确认spring-boot-starter-validation引入的包(如javax.validation相关)与现有依赖无版本冲突(Spring Boot 2.7.x使用javax.validation,避免引入jakarta.validation的包)。
  • 清理IDE缓存:如果使用IDEA,执行File -> Invalidate Caches...并重启;如果是Eclipse,执行Project -> Clean,清理IDE缓存后重新构建项目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 11:30:57