游览器输入URL后出现Request method ‘GET‘ not supported报错

项目场景

项目场景:在用springboot快速起一个增删改查的项目时,在自己的代码使用@DeleteMapping以及@PutMapping注解后,在地址栏输入URL后报错。

	@DeleteMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        bookService.delete(id);
        return "删除成功";
    }

问题描述

在这里插入图片描述
大致意思是:“请求的方法映射异常,该GET请求不被支持。”


原因分析

了解到该问题与请求方式有关,首先要了解相关请求注释。

  • @RequestMapping:该注解没有指定特定请求方式,前端发来请求后该注解可以决定由哪个函数处理。
  • @GetMapping:get的请求方式,是@RequestMapping(method=RequestMethod.GET)的缩写。是浏览器默认的请求方式。
  • @PostMapping:post的请求方式,是@RequestMapping(method=RequestMethod.POST)的缩写。
  • @PutMapping:put的请求方式,@RequestMapping(method=RequestMethod.PUT)的缩写。
  • @DeleteMapping:delete的请求方式,@RequestMapping(method=RequestMethod.DELETE)的缩写。

@GetMapping、@PostMapping、@PutMapping和@DeleteMapping四个注解分别是@RequestMapping注解中get、post、put、delete的派生注解。

游览器的地址栏输入默认的是get方式,相关代码中写的是@DeleteMapping,导致报错。


解决方案

方案一

将相关注解改为@RequestMapping。

	@RequestMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        bookService.delete(id);
        return "删除成功";
    }

方案二

使用Postman,选择相应的请求类型。
在这里插入图片描述

posted @   风起清云  阅读(335)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示