简体   繁体   English

添加 spring 启动验证依赖项后项目运行失败

[英]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:-尝试:-

  • reloading all maven dependencies重新加载所有 maven 依赖项
  • clean install maven build全新安装 maven 构建
  • restart your IDE重新启动您的 IDE
  • delete local.m2 repository and rebuild删除 local.m2 存储库并重建

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.

相关问题 添加 Jpa maven 依赖后无法运行 spring boot app - Cannot run spring boot app after adding Jpa maven dependency 将另一个Spring MVC项目添加为依赖项后,Spring Boot无法找到适当的URL映射 - Spring boot can't find the appropriate URL mapping after adding another spring MVC project as a dependency java.lang.instrument在为弹簧启动执行器添加依赖项后ASSERTION FAILED - java.lang.instrument ASSERTION FAILED after adding dependency for spring boot actuators spring boot中依赖注入失败,基于maven的多模块项目 - Dependency Injection failed in spring boot , maven based multiple modules project 在 spring 引导项目中添加 apache derby 的依赖项时出错 - Error adding the dependency for apache derby in spring boot project 在 maven 依赖项中添加 pubnub 后,Spring Boot 应用程序不启动 - Spring boot application does not start after adding pubnub in maven dependency 添加 Spring Boot Data JPA 依赖会导致 Spring Boot 项目在启动时崩溃 - Adding Spring Boot Data JPA dependency causes Spring Boot project to crash on launch 具有jar依赖项的Spring Boot应用程序在Maven构建后无法运行 - Spring Boot application with a jar dependency does not run after maven build 在Spring Boot项目中找不到依赖项 - Dependency not found in Spring Boot project spring 引导项目中的属性作为依赖项 - Properties in spring boot project as dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM