发生缘由

  • 学习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包下面就可以了。

    在这里插入图片描述
    最后运行,发现没得问题,打开PostMan输入URL也没有任何问题。会在服务器端打印信息,也会在PostMan响应信息。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。