報錯-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但是具有相同模式的服務接口


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM