当前位置:网站首页>“Method Not Allowed“,405问题分析及解决
“Method Not Allowed“,405问题分析及解决
2022-07-28 22:23:00 【jiey0407】
首先,明确一点405问题就是浏览器端(或客户端)请求方法和服务端处理该路径的请求的处理方法不一致造成的。
背景:SpringBoot2.70整合JPA,测试单表的增删改查
现象: 在用postman测试delete方法的时候遇到的
图1: postman测试截图
图2:后端代码块
package com.xxxx.salesforecast.Controller;
import com.xxxx.salesforecast.pojo.User;
import com.xxxx.salesforecast.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户外部接口
*
* @author : liuke
* @date : 2022-06-22 14:20
**/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserRepository userRepository;
/**
* 插入
*
* @param user
* @return
*/
@PostMapping
public String addUser(@RequestBody User user) {
userRepository.save(user);
return "success";
}
/**
* 查询全部
*
* @return
*/
@GetMapping("")
public List<User> getAllUser() {
List<User> userList = userRepository.findAll();
return userList;
}
/**
* 根据 id 删除
* @param id
* @return
*/
@DeleteMapping("/{id}")
public String deleteUserById(@PathVariable Integer id){
userRepository.deleteById(id.intValue());
return "success";
}
}
图三:控制台报错
排查解决过程:
第一步:大致检查一下自己代码,感觉没问题,开始百度
第二步:百度得到的第一个方案,未解决
application.yml中添加spring mvc: hiddenmethod: filter: enabled: true
这个启动HiddenHttpMethodFilter过滤器,以支持浏览器可以发送DELETE PUT 请求。而我没用浏览器发送请求,而是用postman,所以无用也正常。
第三步:尝试将@DeleteMapping改成@RequestMapping,未解决
第四步:尝试去掉@PathVariable 注解,未解决
第五步:想到,我用的是restful接口,增删改查的路径都是一样,通过@PostMapping、@PutMapping、@DeleteMapping、@GeMapping来区分行为。
于是修改了方法的路径,还是不行。但是这个时候不报405了,开始报404了。
404,众所周知一定是没有匹配的路径。
然后我终于发现了问题的所在,下图路径
http://localhost:6060/user/deleteid=1
这里的id是以传参的形式
而@DeleteMapping(“/delete/{id}”)的意思是id当路径
所以路径改成
思考:那为什么之前都会报405的错误呢?
因为匹配到了查询的接口,这个接口就是@GetMapping,所以和@DeleteMapping并不一致,就报了405错误。
且这个接口不需要传参,如果传参了也是不会报错的,比如,我们访问http://baidu.com 和 http://baidu.com/sbbaidu=1是一样的

总结: 问题出在我把@DeleteMapping(“/{id}”)的id当路径和路径传参弄混了。
边栏推荐
- Js判断数据类型的4种⽅式
- 【TA-霜狼_may-《百人计划》】美术2.2 模型基础
- Wildcard ssl/tls certificate
- Leetcode64. 最小路径和
- Powercl batch creates and manages virtual switches
- Uricase - Characteristics of uricase in Worthington pig liver:
- Leetcode64. Minimum path sum
- SQL implementation merges multiple rows of records into one row
- VS2005 accesses the setting method "recommended collection" of vss2005 through sourceoffsite
- Add build dependency error
猜你喜欢

EN 1935建筑五金.单轴铰链—CE认证

实时数仓:网易严选基于Flink的实时数仓实践

实时数仓:美团基于Flink的实时数仓建设实施

PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM

Application of Devops in Internet of things solutions

Detailed principle explanation and verification results of digital clock based on FPGA
![[microservice] Nacos cluster building and loading file configuration](/img/50/7af220c57a06eb186729c9882d9dab.png)
[microservice] Nacos cluster building and loading file configuration

Interpretation of ISO 13400 (doip) standard

Leetcode64. 最小路径和

[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
随机推荐
[detailed and super simple] how to use websocket links
How NAT configures address translation
@Transactional 注解使用详解
SAP temporary tablespace error handling
EN 1935建筑五金.单轴铰链—CE认证
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
Hutool official website (is hutool easy to use)
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
研发效能的道法术器
Word中的\n是什么?:^p
Leetcode62. 不同路径
Real time data warehouse: meituan reviews Flink's real-time data warehouse application sharing
Add build dependency error
Applet editor rich text editing and rich text parsing
Worthington RNA determination detailed introduction
1-6 state与绑定事件
SQL left connection, internal connection writing method "recommended collection"
器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
Leetcode62. Different paths
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming