HTTP请求报错:405 Request method ‘GET‘ not supported解决方法!!(终极整理)

1.问题场景

在项目中发送ajax请求时,控制台提示如下错误信息: org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported

2.异常解析

这个异常翻译过来就是http请求的方法映射异常,后面的详情信息说,该GET请求不被支持。

2.1异常原因

1.该请求路径存在,但是请求类型不为GET请求。

例如:
前端请求:

//前端ajax发送请求
$.ajax({
	url: "http://localhost:8080/test",
	type: "GET",
	data: null,
	datatype: "json",
	success:function(data) {
        console.log(data);
    },
    error:function(err){
       console.log(err);
    }
})

后台controller

	@ResponseBody
    @PostMapping("/test")
    public String test(){
        return "test";
    }

这样子,如果当前的/test路径映射中只存在PostMapping,当我们又使用了GET请求去访问时,就会出现该异常。只需查看当前路径的请求类型,再修改ajax请求中的type属性的值即可。

2.2解决办法
修改ajax请求的type属性
//前端ajax发送请求
$.ajax({
	url: "http://localhost:8080/test",
	type: "POST",  //因为服务器标明是POST请求
	data: null,
	datatype: "json",
	success:function(data) {
        console.log(data);
    },
    error:function(err){
       console.log(err);
    }
})
或者修改服务器的请求为GET请求。
	@ResponseBody
    @GetMapping("/test")
    public String test(){
        return "test";
    }

3.总结

当出现org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘XXX’ not supported异常时,检查前端发送请求的路径是否与服务器映射路径上标明的请求类型对应。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值