![](/img/trans.png)
[英]Cannot run spring boot app after adding Jpa maven dependency
[英]Project failed to run after adding spring boot validation dependency
我正在做一個小項目。 其中使用 mongoDB 作為數據庫。 我想在我的Player
bean 上添加驗證。 但是只要我添加以下依賴項:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
在我的pom.xml文件中。
當我作為 spring 啟動應用程序運行時,我的項目開始失敗。
這是我的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 啟動驗證依賴項,我的項目不會給出任何錯誤。
如果有人可以向我解釋這里出了什么問題,那將非常有幫助。
I understand that you have imported your project from spring initializr and your pom.xml looks same as this spring.io link:-
理想情況下,您不應該面對ClassNotFoundException
。 嘗試:-
如果這不起作用,請嘗試添加以下依賴項並構建項目:-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
您是否嘗試過清理和構建應用程序然后運行它? 如果它不起作用,那么您是否嘗試使用 @SpringBootApplication 注釋“運行文件”?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.