Springboot运行报错: "status": 404, "error": "Not Found"

发生缘由

发生缘由如下:

  • 学习SpringBoot

运行环境

  • jdk版本:jdk-16.0.2

  • SpringBoot版本:2.7.0

  • PostMan版本:8.3.1

  • Idea版本:2021.2

  • 电脑系统:win10

环境搭建

  1. 创建一个SpringBoot项目

  2. 编写一个Controller类

    @RestController
    @RequestMapping("/books")
    public class BookController {
    ​
        @GetMapping("/{id}")
        public String getById(@PathVariable Integer id) {
            System.out.println("id -->" + id);
            return "hello springboot";
        }
    }

  3. 运行创建的SpringBoot项目,打开PostMan输入如下URL:http://localhost:8080/books/1

  4. 结果发现报错

问题补救

报错信息如下:

{
    "timestamp": "2022-05-27T10:39:14.562+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/books/1"
}

解决方案如下:

  • 发现将主方法所在的类的位置弄错了,其实只要将该类直接放到com.linxuan包下面就可以了。

    最后运行,发现没得问题

posted @ 2022-05-27 19:02  炸天帮帮主  阅读(204)  评论(0编辑  收藏  举报