邮递员返回“状态”:404,“错误”:“未找到”,用于春季启动

时间:2021-07-09 06:59:11

标签: java spring spring-boot spring-mvc spring-data-jpa

在此先感谢您,您好,我是 Spring Boot 的新手。我正面临着弹簧靴的问题。当我在邮递员中运行我的 URL 时,它显示以下错误。我也在下面上传了我的代码。我不知道我的代码有什么问题。

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

My project structure

控制器代码 这是我的控制器代码的代码。

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);
    
}

}

型号

我的模型类的代码 包 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;
    }

    
    

}

回购 我的回购类的代码

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> {

}

主类

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 我的 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.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>

2 个答案:

答案 0 :(得分:0)

您的映射存在问题。您没有指定映射路径,因此 findAll 和 save 将默认映射设置为空,然后控制器无法理解您调用的是哪个 API。添加以下内容并尝试。

@GetMapping(value="/findAll")

@PostMapping(value=/save")

答案 1 :(得分:0)

@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;
}
}

你像这样给方法映射。您可以提供任何 URL 名称。这是您收到 404 错误的主要原因。