项目场景:
1、java版本:8.0
2、SpringBoot版本:2.6.7
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/>
</parent>
3、IDE:IntelliJ IDEA 2019
4、项目主要代码
(1)thymeleaf模板
<form th:action="@{/emp/}+${emp.id}" method="post">
<input type="hidden" name="_method" value="delete"/>
<button type="submit" class="btn btn-sm btn-danger">删除</button>
</form>
(2)控制层代码
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
//Rest风格,以delete方式,实现员工的删除
@DeleteMapping("/emp/{id}")
// @RequestMapping("/emp/{id}")
public String deleteEmp(@PathVariable("id") Integer id){//接收前端URL携带的id
employeeDao.delete(id);
return "redirect:/emps";
}
}
问题描述
运行项目,直接报405......
原因分析:
个人猜测:高版本的Springboot已经不支持@DeleteMapping、@PutMapping等Rest风格注解了,而直接用@RequestMapping代替。(纯属个人猜测哈)
解决方案:
方案一:把@DeleteMapping换成@RequestMapping
@RequestMapping("/emp/{id}")
public String deleteEmp(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
方案二:把高版本SpringBoot换成低一点的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>