簡體   English   中英

postman 返回“狀態”:404,“錯誤”:“未找到”,用於 spring 引導

[英]postman returning "status": 404, "error": "Not Found", for the spring boot

提前謝謝你,你好,我是 spring 引導的新手。 我正面臨 spring 引導的問題。 當我在 postman 中運行我的 URL 時,它顯示以下錯誤。 我還在下面上傳了我的代碼。 我不知道我的代碼有什么問題。

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

我的項目結構

Controller代碼這是我的controller代碼的代碼。

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

我的代碼 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;
    }

    
    

}

我的回購代碼 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> {

}

主類

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>

您的映射存在問題。 您沒有指定映射路徑,因此 findAll 和 save 將默認映射設置為空,然后控制器無法理解您調用的是哪個 API。 添加以下內容並嘗試。

@GetMapping(value="/findAll")

@PostMapping(value=/save")
@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 錯誤的主要原因。

TodoApplication class 必須在這一層之上。

改變這個:

package com.assignment.todo.Todo;

package com.assignment.todo;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM