Request method ‘DELETE‘ is not supported

如图,今天在学习 springboot 时犯了个很低级的错,遇到了这个错误。
错误信息

背景

前端

// 文章删除
export const articleCategoryDeleteService = (categoryId) => {  
    return request.delete('/category?id=' + categoryId)
}

后端

@DeleteMapping("/category/categoryId")
public Result delete(@PathParam("categoryId") Integer id){
    categoryService.delete(id);
    return Result.success();
}

解决

将后端代码中的 categoryId 去掉

@DeleteMapping("/category")
public Result delete(@PathParam("categoryId") Integer id){
    categoryService.delete(id);
    return Result.success();
}

@PathVariable 和 @PathParam

这里顺便记录下 @PathVariable 和 @PathParam 获取参数的方式,例如前端请求路径为

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

此时后端的方法为

@RequestMapping("/hello/{id}")
public String getDetails(@PathVariable(value="id") String id,
    			@RequestParam(value="param1", required=true) String param1,
    			@RequestParam(value="param2", required=false) String param2){
				.......
}

若参数为从URL 模板中获取,例如 ***/user/1,那么在后端要获取 1 这个参数是就要用 @PathVariable,且在后端方法中要写上 ({/id}) 形式的参数,而如果是 ?param10=1&param2=20 这种的则需要用 @PathParam 接受,并且在后端方法中不用携带相对应的参数。

总结

如果只是 ID 这种单个或者多个数字字母,使用 @PathVariable 是非常好的选择,但是如果是获取多个参数,而且是不同类型的,那么最好使用 @PathParam。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值