国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例

springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例

2021-02-20 11:20RexFang Java教程

這篇文章主要介紹了springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例,rest風(fēng)格簡(jiǎn)潔,分享了HiddenHttpMethodFilter 的源碼,通過(guò)Spring4.0實(shí)現(xiàn)rest風(fēng)格源碼及簡(jiǎn)單錯(cuò)誤分析,具有一定參考價(jià)值,需要的朋友可以了解下。

簡(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 開始支持 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 源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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(大寫) 即可。詳細(xì)例子如下:

在 web.xml 中配置 HiddenHttpMethodFilter
編寫 handler 代碼
編寫頁(yè)面

?
1
2
3
4
5
6
7
8
9
<!-- 配置 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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ 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ò)誤。

springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例

總結(jié)

以上就是本文關(guān)于springmvc Rest風(fēng)格介紹及實(shí)現(xiàn)代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。

原文鏈接:https://www.cnblogs.com/rexfang/p/6583266.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美大逼| 久久在线| 一级黄片毛片免费看 | 美日韩一区| 久久国产精品视频 | 亚洲免费视频一区 | 亚洲精品字幕 | 成人在线中文字幕 | 久久av网 | 懂色av成人一区二区三区 | 日韩不卡一区二区三区 | 国产福利91精品一区二区三区 | 99精品在线观看 | 日日夜夜av | 亚洲乱码国产乱码精品精的特点 | 嫩草影院黄色 | 日本手机在线视频 | 欧美日韩免费在线 | 欧美精品一级二级 | 91久久国产综合久久 | 国产精品成人一区二区三区夜夜夜 | 丝袜久久 | 99久久综合精品五月天 | 精品国产欧美一区二区三区成人 | 欧美成人高清视频 | 综合久久久 | 欧美精三区欧美精三区 | 中文字幕在线免费看 | 爱色av网站 | 午夜精品在线 | 日韩精品久久久久久 | 美女一级| 亚洲欧美日韩在线 | 国内自拍视频在线观看 | 亚洲成a人v欧美综合天堂麻豆 | 精品国产一区二区三区在线观看 | 亚洲高清色综合 | 99久久免费精品 | 黄色毛片网站在线观看 | 伊人婷婷| 日韩精品免费 |