springBoot使用Restful跳轉路徑回顯異常問題
阿新 • • 發佈:2020-08-27
在一次使用SpringBoot+thymelaf+RESTful進行資料刪除時,資料刪除完成後,資料進行回顯異常。
controller核心程式碼
//分頁回顯資料 @RequestMapping("/getEmpList") public String getEmpPage(Page<Emp> page, Model model){ System.err.println("EmpController.getEmpPage"); Page<Emp> pageBean = service.page(page); model.addAttribute("page",pageBean); model.addAttribute("url","getEmpList"); return "empList"; } //根據id刪除 @RequestMapping("/deleteEmpById/{id}") public String deleteById(@PathVariable Integer id){ boolean b = service.removeById(id); return "redirect:/getEmpList"; }
前端頁面
點選刪除後
注意看這裡的地址為:
http://localhost:8080/deleteEmpById/getEmpList
而資料回顯的地址為:
http://localhost:8080/getEmpList,報了一個500錯誤,當然,我這裡進行了全域性異常處理,以JSON的格式返回。
錯誤原因為刪除使用的是RESTful風格,即地址為:http://localhost:8080/deleteEmpById/${id},這裡將getEmpList當成了id,所以造成型別轉換異常
找了半個小時才找到,小夥伴們以後千萬要注意哦!!!