报错-Missing URI template variable 'xxx' for method parameter of type Long

起因

使用postman测试后端接口

{
    "timestamp": "2020-11-18T11:28:51.219+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Missing URI template variable 'xxx' for method parameter of type Long",
    "path": "/spec/params"
}

服务器异常,出现“丢失long类型的方法参数的链接模板值xxx”错误

原因

使用了@PathVariable注解,但没有使用RestFul风格写法

解决方案

 // @GetMapping("/params")  原写法
 @GetMapping("/params/{xxx}")

梳理@RequestParma/@PathVariable

相同点

两者都可以获取参数,都可以设置参数是否为必填和默认值

不同点

@RequestParam是真的只能请求参数
@PathVariable是参数变量,用范围更宽广,可以使用正则,将url分割为参数,而不仅仅是获取请求中携带的参数,可以应用于url匹配

例如项目中匹配不同url但是具有相同模式的服务接口

原文地址:https://www.cnblogs.com/lifelikeplay/p/14001730.html