首页 > 技术文章 > springBoot使用Restful跳转路径回显异常问题

codeli 2020-08-26 19:57 原文

在一次使用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,所以造成类型转换异常

找了半个小时才找到,小伙伴们以后千万要注意哦!!!

推荐阅读