'postman returning "status": 404, "error": "Not Found", for the spring boot

Thank you in advance, Hello I am new to spring boot. I am facing a problem with spring boot. When I run my URL in postman it shows an error below. I have also uploaded my code below. I don't find any idea what wrong with my code.

 {
    "timestamp": "2021-07-09T06:43:57.429+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/todo"
}

My project structure

Controller code This is a code for my controller code.

package com.assignment.todo.Todo.Controller;
import com.assignment.todo.Todo.Repo.*;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.assignment.todo.Todo.Model.*;
import com.assignment.todo.Todo.Repo.*;

@RestController
@RequestMapping(value="/todo")
public class TodoController  {

@Autowired
private TodoRepo todoRepo;

@GetMapping
public List<TodoModel> findAll(){
    return todoRepo.findAll();
    
}
    
@PostMapping
public TodoModel save(@Valid @NotNull @RequestBody TodoModel todoItem) {
    return todoRepo.save(todoItem);
    
}

}

Model

code for my model class package com.assignment.todo.Todo.Model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.validation.constraints.NotBlank;

import org.springframework.data.annotation.Id;
@Entity
public class TodoModel {
    
    private Long id;
    @NotBlank
    private String title;
    private boolean done;
    
    public TodoModel() {
        
    }
    
    public TodoModel(long id, String title, boolean done) {
        this.id=id;
        this.title=title;
        this.done=done;
    }
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    
    

}

Repo code for my repo class

package com.assignment.todo.Todo.Repo;
import com.assignment.todo.Todo.Model.*;
import org.springframework.data.jpa.repository.JpaRepository;



public interface TodoRepo extends JpaRepository<TodoModel, Long> {

}

MainClass

package com.assignment.todo.Todo;
import com.assignment.todo.Todo.Controller.*;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.assignment.todo.Todo.Repo.*;


@SpringBootApplication(scanBasePackages = "TodoRepo.java")
public class TodoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TodoApplication.class, args);
        
    }

}

pom.xml My pom.xml file

<?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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.assignment.todo</groupId>
    <artifactId>Todo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Todo</name>
    <description>Assignment in Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
             <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
          <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepositosry.com/artifact/org.springframework/spring-web -->
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
        <!-- https://mvnrepository.com/artifact/com.sun.istack/maven-istack-commons-plugin -->
<!-- https://mvnrepository.com/artifact/com.sun.istack/maven-istack-commons-plugin -->
<dependency>
    <groupId>com.sun.istack</groupId>
    <artifactId>maven-istack-commons-plugin</artifactId>
    <version>2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.5.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        
    </build>

</project>


Solution 1:[1]

@RestController("/todo")// you can give any URL here
public class TodoController  {

@Autowired
private TodoRepo todoRepo;

@GetMapping("/getAll")// you can give any URL here
public List<TodoModel> findAll(){
    return todoRepo.findAll();
    
}
    
@PostMapping("/save")// you can give any URL here
public TodoModel save(@Valid @NotNull @RequestBody TodoModel todoItem) {
    todoRepo.save(todoItem);
    return todoItem;
}
}

you give the mapping to the method like this. you can give any URL name. This is the main reason you get a 404 error.

Solution 2:[2]

There is issue in your mapping. You didn't specify the mapping path so findAll and save has default mapping as empty then controller could not understand which API you calling. Add below things and try.

@GetMapping(value="/findAll")

@PostMapping(value=/save")

Solution 3:[3]

The TodoApplication class must be above this one level.

change this:

from

package com.assignment.todo.Todo;

to

package com.assignment.todo;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kaushik Shaw
Solution 2 S. Anushan
Solution 3 fatih