Home > Blockchain >  Project failed to run after adding spring boot validation dependency
Project failed to run after adding spring boot validation dependency

Time:09-16

I'm working on a small project. Which uses mongoDB as DB. I want to add validation on my 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.

My project starts failing when I run as spring boot app.

Here is my 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:

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.

It will be really helpful if anybody can explain me what is going wrong here.

CodePudding user response:

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?

CodePudding user response:

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 project for Spring Boot&packageName=com.example.demo&dependencies=data-mongodb,web,devtools,validation

Ideally you should not face ClassNotFoundException. Try:-

  • reloading all maven dependencies
  • clean install maven build
  • restart your IDE
  • delete local .m2 repository and rebuild

If that doesn't work, try adding following dependency and build project:-

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>
  • Related