springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例
簡(jiǎn)介
REST 即 Representational State Transfer。(資源)表現(xiàn)層狀態(tài)轉(zhuǎn)化。是目前最流行的一種互聯(lián)網(wǎng)軟件架構(gòu)。它結(jié)構(gòu)清晰、符合標(biāo)準(zhǔn)、易于理解、擴(kuò)展方便,所以正得到越來(lái)越多網(wǎng)站的采用,POST, DELETE, PUT, GET 分別對(duì)應(yīng) CRUD。Spring3.0 開(kāi)始支持 REST 風(fēng)格的請(qǐng)求,是通過(guò) org.springframework.web.filter.HiddenHttpMethodFilter 把 POST 請(qǐng)求轉(zhuǎn)化為 PUT 和 DELETE 請(qǐng)求。本次實(shí)驗(yàn)采用的是 Spring4.0 。
HiddenHttpMethodFilter 源碼
public static final String DEFAULT_METHOD_PARAM = "_method";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String paramValue = request.getParameter(this.methodParam);
if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
filterChain.doFilter(wrapper, response);
} else {
filterChain.doFilter(request, response);
}
}
從 HiddenHttpMethodFilter 的源碼可以看出,Spring 根據(jù)請(qǐng)求中的 _method 參數(shù)進(jìn)行轉(zhuǎn)化,因此如果想發(fā)起 REST 風(fēng)格的 DELETE 或者 PUT 請(qǐng)求,只需要在表單中帶上 _method 參數(shù),并且把 _method 的值設(shè)置為 DELETE 或者 PUT(大寫(xiě)) 即可。詳細(xì)例子如下:
在 web.xml 中配置 HiddenHttpMethodFilter
編寫(xiě) handler 代碼
編寫(xiě)頁(yè)面
<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter ,可以把 POST 請(qǐng)求轉(zhuǎn)化為 PUT 或者 DELETE 請(qǐng)求 -->
<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>
package rex.springmvc.handlers;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping(value="/restTest")
@Controller
public class RestTestHandler {
private static final Logger logger = Logger.getLogger(RestTestHandler.class);
private static final String SUCCESS = "success";
@RequestMapping(value="/restGet/{id}", method=RequestMethod.GET)
public String restGet(@RequestParam(value="id", required=false) Integer id){
logger.debug("restGet:" + id);
return SUCCESS;
}
@RequestMapping(value="/restPut/{id}", method=RequestMethod.PUT)
public String restPut(@RequestParam(value="id", required=false) Integer id){
logger.debug("restPut:" + id);
return SUCCESS;
}
@RequestMapping(value="/restDelete/{id}", method=RequestMethod.DELETE)
public String restDelete(@RequestParam(value="id", required=false) Integer id){
logger.debug("restDelete:" + id);
return SUCCESS;
}
@RequestMapping(value="/restPost", method=RequestMethod.POST)
public String restPost(){
logger.debug("restPost");
return SUCCESS;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rest Test</title>
</head>
<body>
<br>
<br>
<a href="restTest/restGet/1" rel="external nofollow" >Test Rest Get</a>
<br>
<br>
<form action="restTest/restPut/1" method="post">
<input type="hidden" name="_method" value="PUT"> <input
type="submit" value="submit">
</form>
<br>
<br>
<form action="restTest/restDelete/1" method="post">
<input type="hidden" name="_method" value="DELETE"> <input
type="submit" value="submit">
</form>
<br>
<br>
<form action="restTest/restPost" method="post">
<input type="submit" value="submit">
</form>
</body>
</html>
注:handler 中 @RequestParam 注解必須加上 required 參數(shù),否則訪問(wèn)頁(yè)面會(huì)出現(xiàn)400錯(cuò)誤。

總結(jié)
以上就是本文關(guān)于springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
SpringMVC開(kāi)發(fā)restful API之用戶查詢代碼詳解
SpringMVC使用MultipartFile 實(shí)現(xiàn)異步上傳方法介紹
如有不足之處,歡迎留言指出。
- springmvc使用REST出現(xiàn):Request?method?'PUT'?not?supported問(wèn)題
- 如何利用Spring?MVC實(shí)現(xiàn)RESTful風(fēng)格
- SpringMVC開(kāi)發(fā)restful API之用戶查詢代碼詳解
- Spring MVC利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API詳解
- SpringMVC Restful api接口實(shí)現(xiàn)的代碼
- SpringMVC數(shù)據(jù)頁(yè)響應(yīng)ModelAndView實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Spring MVC 文件、cookies的接收 與REST響應(yīng)詳解
相關(guān)文章
淺談Springmvc中的頁(yè)面跳轉(zhuǎn)問(wèn)題
這篇文章主要介紹了淺談Springmvc中的頁(yè)面跳轉(zhuǎn)問(wèn)題,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12
在IDEA中安裝MyBatis Log Plugin插件,執(zhí)行mybatis的sql語(yǔ)句(推薦)
這篇文章主要介紹了在IDEA中安裝MyBatis Log Plugin插件,執(zhí)行mybatis的sql語(yǔ)句,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
mybatis-plus 新增/修改如何實(shí)現(xiàn)自動(dòng)填充指定字段
這篇文章主要介紹了mybatis-plus 新增/修改實(shí)現(xiàn)自動(dòng)填充指定字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java版微信公眾號(hào)支付開(kāi)發(fā)全過(guò)程
這篇文章主要介紹了Java版微信公眾號(hào)支付開(kāi)發(fā)全過(guò)程,本文通過(guò)實(shí)例相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
java POI解析Excel 之?dāng)?shù)據(jù)轉(zhuǎn)換公用方法(推薦)
下面小編就為大家?guī)?lái)一篇java POI解析Excel 之?dāng)?shù)據(jù)轉(zhuǎn)換公用方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控
這篇文章主要介紹了如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring @CrossOrigin 注解原理實(shí)現(xiàn)
這篇文章主要介紹了Spring @CrossOrigin 注解原理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java使用反射和動(dòng)態(tài)代理實(shí)現(xiàn)一個(gè)View注解綁定庫(kù)
這篇文章主要介紹了Java使用反射和動(dòng)態(tài)代理實(shí)現(xiàn)一個(gè)View注解綁定庫(kù),代碼簡(jiǎn)潔,使用簡(jiǎn)單,擴(kuò)展性強(qiáng),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

