SpringMVC的REST風格的四種請求方式總結(jié)
一、 在HTTP 協(xié)議里面,四個表示操作方式的動詞:GET、POST、PUT、DELETE。
它們分別對應四種基本操作:
1、GET ====== 獲 取資源
2、POST ======新建資源
3、PUT======= 更新資源
4、DELETE==== 刪除資源
二、REST:即 Representational State Transfer。(資源)表現(xiàn)層狀態(tài)轉(zhuǎn)化。是目前最流行的一種互聯(lián)網(wǎng)軟件架構(gòu)。它結(jié)構(gòu)清晰、符合標準、易于理解、擴展方便, 所以正得到越來越多網(wǎng)站的采用。
我們可以通過rest風格占位符方式,利用@PathVariable注解將占位符的值賦給調(diào)用方法參數(shù),實現(xiàn)結(jié)果:
/某路徑/1 HTTP GET : 得到 id = 1 的 一條數(shù)據(jù)
/某路徑/1 HTTP DELETE: 刪除 id = 1的 一條數(shù)據(jù)
/某路徑/1 HTTP PUT: 更新id = 1的 一條數(shù)據(jù)
/某路徑 HTTP POST: 新增一條數(shù)據(jù)
實現(xiàn)方式(REST風格四種請求方式的調(diào)用):
我們通過@RequestMapping映射請求中的method參數(shù)實現(xiàn)四種請求方式的調(diào)用,以下為示例代碼。
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請求轉(zhuǎn)化為put請求和delele請求
1.在web.xml文件中配置HiddenHttpMethodFilter過濾器:
<!-- HiddenHttpMethodFilter過濾器可以將POST請求轉(zhuǎn)化為put請求和delete請求! --> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.在表單域中需要攜帶一個name值為_method,value值為put或者delete的參數(shù),如下所示:
<form action="" method="post"> <input type="hidden" name="_method" value="delete"> </form>
<form:form action="${pageContext.request.contextPath}/student" method="post" modelAttribute="employee">
<c:if test="${empty employee.id }">
姓名:<form:input path="lastName"/><br>
</c:if>
<c:if test="${!empty employee.id }">
姓名:<form:input path="lastName" readonly="true"/><br>
<form:hidden path="id"/>
<input type="hidden" name="_method" value="put">
</c:if>
郵箱:<form:input path="email"/><br>
<%
Map<String,Object>map=new HashMap<String,Object>();
map.put("1","Male");
map.put("0", "Female");
request.setAttribute("genders", map);
%>
性別:<form:radiobuttons path="gender" items="${genders}"/><br>
部門:<form:select path="department.id" items="${departments}" itemValue="id" itemLabel="departmentName"></form:select><br>
<input type="submit" value="提交">
</form:form>
最后在Controller層調(diào)用即可。根據(jù)@RequestMapping的value值以及攜帶的參數(shù)、請求方式查找匹配函數(shù)。
以上這篇SpringMVC的REST風格的四種請求方式總結(jié)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring boot通過HttpSessionListener監(jiān)聽器統(tǒng)計在線人數(shù)的實現(xiàn)代碼
這篇文章主要介紹了Spring boot通過HttpSessionListener監(jiān)聽器統(tǒng)計在線人數(shù)的實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02
ShardingSphere JDBC強制路由使用的項目實踐
在某些特定場景下,可能需要繞過分片規(guī)則直接定位到特定的數(shù)據(jù)庫或表,這種情況下就可以使用HintRouting,本文就來介紹一下ShardingSphere JDBC強制路由使用的項目實踐,感興趣的可以了解一下2024-06-06
IntelliJ IDEA中顯示和關(guān)閉工具欄與目錄欄的方法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中顯示和關(guān)閉工具欄與目錄欄的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
ElasticSearch 動態(tài)映射實戰(zhàn)詳解
這篇文章主要為大家介紹了ElasticSearch 動態(tài)映射實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
java使用TimerTask定時器獲取指定網(wǎng)絡數(shù)據(jù)
java.util.Timer定時器,實際上是個線程,定時調(diào)度所擁有的TimerTasks。一個TimerTask實際上就是一個擁有run方法的類,需要定時執(zhí)行的代碼放到run方法體內(nèi),TimerTask一般是以匿名類的方式創(chuàng)建,下面的就用示例來學習他的使用方法2014-01-01
linux的shell命令檢測某個java程序是否執(zhí)行
ps -ef |grep java|grep2016-04-04

