直接上干货:
我们知道在我们之前的Spring 中若 想要开启我们的REST风格,使用表单PUT和DELETE请求,我们就必须配置过滤,很是麻烦。
但是我们在SpringBoot中如果想要开启,只需要在我们yaml或者properties配置文件中开启:
spring:
mvc:
hiddenmethod:
filter:
enabled: true
即可开启我们SpringBoot请求
紧接着我们在我们的表单中需要添加一个隐藏域来表示我们的请求方式,其中name必须为_method,(我们也可以后期修改)而value即为我们想要的操作
修改name的默认值
我们默认在SpringBoot中是不开启的,因为我们通常在开发中只有用到表单的时候才会需要开启。因为表单中是没有POST和DELETE请求。所以就需要开启。
例如:
(代码实现)
前端代码:
后端:
@RestController
public class helloController {
@RequestMapping("scenery.jpg")
public String test01(){
return "tupian";
}
@GetMapping("/user")
//我们可以使用注解XXXMapping(“value”)来代替我们的@RequestMapping(value= “XX”,method = RequestMethod.XXX)
public String test02(){
return "GET_ebdbbd";
}
@PostMapping("/user")
public String test03(){
return "POST_ebdbbd";
}
@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String test04(){
return "DELETE_ebdbbd";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String test05(){
return "PUT_ebdbbd";
}
}
yml配置文件:
spring:
mvc:
hiddenmethod:
filter:
enabled: true
而在开发中,我们通常是与我们前端进行分离的,前端给我们一个接口,我们只需要给前端返回一个JSON数据即可。
REST原理的实现:



