我在做一个小项目。它使用MongoDB作为数据库。我想在我的Player
Bean上添加验证。但只要我添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
在我的pom.xml文件中。
当我作为Spring Boot应用程序运行时,我的项目开始失败。
下面是我的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
Bean:
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
但是,如果我删除了Spring Boot验证依赖项,我的项目不会给出任何错误。
如果有人能给我解释一下这里出了什么问题,那将是非常有帮助的。
2条答案
按热度按时间66bbxpm51#
我知道您已经从Spring Initializr导入了您的项目,并且您的pom.xml看起来与这个spring.io链接相同:-
Https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.7.3&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=data-mongodb,Web、开发工具、验证
理想情况下,您不应该面对
ClassNotFoundException
。尝试:-如果这不起作用,请尝试添加以下依赖项并构建项目:-
u0njafvf2#
您是否尝试过清理和构建应用程序,然后运行它?如果它不起作用,那么您是否尝试过使用@SpringBootApplication注解来“运行文件”?