[英]Project failed to run after adding spring boot validation dependency
I'm working on a small project.我正在做一个小项目。 Which uses mongoDB as DB.
其中使用 mongoDB 作为数据库。 I want to add validation on my
Player
bean.我想在我的
Player
bean 上添加验证。 But as soon as I add below dependency:但是只要我添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
In my pom.xml file.在我的pom.xml文件中。
My project starts failing when I run as spring boot app.当我作为 spring 启动应用程序运行时,我的项目开始失败。
Here is my pom.xml :这是我的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>
Here is my Player
bean:这是我的
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); } }
The error I'm getting after adding the validation dependency:添加验证依赖项后出现的错误:
Error: Could not find or load main class com.classhacker.tttapi.DemoApplication
Caused by: java.lang.ClassNotFoundException: com.classhacker.tttapi.DemoApplication
But if I remove spring boot validation dependency my project doesn't give any error.但是,如果我删除 spring 启动验证依赖项,我的项目不会给出任何错误。
It will be really helpful if anybody can explain me what is going wrong here.如果有人可以向我解释这里出了什么问题,那将非常有帮助。
I understand that you have imported your project from spring initializr and your pom.xml looks same as this spring.io link:- I understand that you have imported your project from spring initializr and your pom.xml looks same as this spring.io link:-
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,devtools,validation 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,devtools,验证
Ideally you should not face ClassNotFoundException
.理想情况下,您不应该面对
ClassNotFoundException
。 Try:-尝试:-
If that doesn't work, try adding following dependency and build project:-如果这不起作用,请尝试添加以下依赖项并构建项目:-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Have you tried to clean and build the application and then run it?您是否尝试过清理和构建应用程序然后运行它? If it did not work then have you tried to "run file" with your @SpringBootApplication annotation?
如果它不起作用,那么您是否尝试使用 @SpringBootApplication 注释“运行文件”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.