Rest风格
rest风格就是同一路径对应不同提交方式实现不同的功能,提交方式有四种
- post
- get
- put
- delete
代码实现
源码中发现我们需要配置一个
mvc: hiddenmethod: filter: enabled: true
然后在表单中多提交一个隐藏项,因为表单提交只支持get和post
<form action="/user" METHOD="post">
<input type="hidden" name="_method" value="put"/>
<input type="submit" value="put">
</form>
<form action="/user" method="post">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" value="delete">
</form>
这里注意要把提交方式写成post
否则默认是get就不生效了
最后简单写个Controller
package cn.zjq.controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class RestfulController {
@PostMapping("/user")
public String post(){
return "post";
}
@GetMapping("/user")
public String get(){
return "get";
}
@RequestMapping(value = "/user" ,method = RequestMethod.PUT)
public String put(){
return "put";
}
@RequestMapping(value= "/user",method = RequestMethod.DELETE)
public String delete(){
return "delete";
}
}