您好,登錄后才能下訂單哦!
這篇文章主要介紹了springmvc如何實現Rest風格,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
簡介
REST 即 Representational State Transfer。(資源)表現層狀態轉化。是目前最流行的一種互聯網軟件架構。它結構清晰、符合標準、易于理解、擴展方便,所以正得到越來越多網站的采用,POST, DELETE, PUT, GET 分別對應 CRUD。Spring3.0 開始支持 REST 風格的請求,是通過 org.springframework.web.filter.HiddenHttpMethodFilter 把 POST 請求轉化為 PUT 和 DELETE 請求。本次實驗采用的是 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 根據請求中的 _method 參數進行轉化,因此如果想發起 REST 風格的 DELETE 或者 PUT 請求,只需要在表單中帶上 _method 參數,并且把 _method 的值設置為 DELETE 或者 PUT(大寫) 即可。詳細例子如下:
在 web.xml 中配置 HiddenHttpMethodFilter
編寫 handler 代碼
編寫頁面
<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter ,可以把 POST 請求轉化為 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>
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 參數,否則訪問頁面會出現400錯誤。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“springmvc如何實現Rest風格”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。