javascript
java rest风格传参_SpringMVC的REST风格的四种请求方式总结
一、 在HTTP 協議里面,四個表示操作方式的動詞:GET、POST、PUT、DELETE。
它們分別對應四種基本操作:
1、GET ====== 獲 取資源
2、POST ======新建資源
3、PUT======= 更新資源
4、DELETE==== 刪除資源
二、REST:即 Representational State Transfer。(資源)表現層狀態轉化。是目前最流行的一種互聯網軟件架構。它結構清晰、符合標準、易于理解、擴展方便, 所以正得到越來越多網站的采用。
我們可以通過rest風格占位符方式,利用@PathVariable注解將占位符的值賦給調用方法參數,實現結果:
/某路徑/1 HTTP GET : 得到 id = 1 的 一條數據
/某路徑/1 HTTP DELETE: 刪除 id = 1的 一條數據
/某路徑/1 HTTP PUT: 更新id = 1的 一條數據
/某路徑 HTTP POST: 新增一條數據
實現方式(REST風格四種請求方式的調用):
我們通過@RequestMapping映射請求中的method參數實現四種請求方式的調用,以下為示例代碼。
GET請求:
@RequestMapping(value="/student",method=RequestMethod.GET)
public ModelAndView toAddPage(){
ModelAndView mView=new ModelAndView();
mView.addObject("employee",new Employee());
mView.setViewName("add-stu");
mView.addObject("departments", departmentDao.getDepartments());
return mView;
}
POST請求:
@RequestMapping(value="/student",method=RequestMethod.POST)
public String addStu(Employee employee){
employeeDao.save(employee);
return "redirect:/show" ;
}
DELETE請求:
@RequestMapping(value="/student/{id}",method=RequestMethod.DELETE)
public String deleteStu(@PathVariable(value="id") Integer id){
employeeDao.delete(id);
return "redirect:/show" ;
}
PUT請求:
@RequestMapping(value="/student",method=RequestMethod.PUT)
public String Update(@RequestParam(value="id")Integer id,Employee employee){
employeeDao.save(employee);
return "redirect:/show" ;
}
三、將POST請求轉化為put請求和delele請求
1.在web.xml文件中配置HiddenHttpMethodFilter過濾器:
hiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
hiddenHttpMethodFilter
/*
2.在表單域中需要攜帶一個name值為_method,value值為put或者delete的參數,如下所示:
姓名:
姓名:
郵箱:
Mapmap=new HashMap();
map.put("1","Male");
map.put("0", "Female");
request.setAttribute("genders", map);
%>
性別:
部門:
最后在Controller層調用即可。根據@RequestMapping的value值以及攜帶的參數、請求方式查找匹配函數。
以上這篇SpringMVC的REST風格的四種請求方式總結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。
時間: 2017-08-28
總結
以上是生活随笔為你收集整理的java rest风格传参_SpringMVC的REST风格的四种请求方式总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RxJS——可观察的对象(Observa
- 下一篇: springcloud 整合 gatew