【spring springmvc】這裡有你想要的SpringMVC的REST風格的四種請求方式

  • 2020 年 3 月 29 日
  • 筆記

概述

之前的文章springmvc使用註解聲明控制器與請求映射有簡單提到過控制器與請求映射,這一次就詳細講解一下SpringMVCREST風格的四種請求方式及其使用方法。

你能get的知識點

1、什麼是Rest風格?
2、基於springmvc實現REST風格的四種請求方式
3、post請求轉換為deleteput請求
4、解決請求亂碼問題
5、RequestMapping註解的屬性
@

壹:rest風格

一:什麼是Rest風格?

REST即表述性狀態傳遞(英文:Representational State Transfer,簡稱REST)是Roy Fielding博士在2000年他的博士論文中提出來的一種軟體架構風格。它是一種針對網路應用的設計和開發方式,可以降低開發的複雜性,提高系統的可伸縮性。

簡單來說:使用URL定位資源,用HTTP動詞(例如GET,POST,DELETE,DETC等)描述操作。

二:REST風格的四種請求方式

請求 說明 用於 例子 例子說明
@GetMapping 匹配GET方式的請求; 一般用於讀取數據 /user/1 獲取一號用戶資訊
@PostMapping 匹配POST方式的請求; 一般用於新增數據 /user/1 新增一號用戶
@PutMapping 匹配PUT方式的請求; 一般用於更新數據 /user/1 修改一號用戶
@DeleteMapping 匹配DELETE方式的請求; 一般用於刪除數據 /user/1 刪除一號用戶

也就是說,我們不再使用/user/getuser?user=1/user/deleteuser?user=1等來區分使用者的行為操作,而是使用不同的請求方式來描述行為。

貳:基於springmvc實現REST風格的四種請求方式

Spring框架的4.3版本後,引入了新的組合註解,來幫助簡化常用的HTTP方法的映射,並更好的表達被註解方法的語義。

@GetMapping = @requestMapping(method = RequestMethod.GET)。  @PostMapping = @requestMapping(method = RequestMethod.POST)。  @DeleteMapping = @requestMapping(method = RequestMethod.DELETE)。  @PutMapping = @requestMapping(method = RequestMethod.PuT)。  

一:@GetMapping請求

@GetMapping為例,該組合註解是@RequestMapping(method = RequestMethod.GET)的縮寫,它會將HTTP GET請求映射到特定的處理方法上。

RequestMapping後所有屬性都是可選的,但其默認屬性是value。當value是其唯一屬性時,可以省略屬性名。

@RequestMapping(value = "/rest",method = RequestMethod.GET)  public String restGet(){  	System.out.println("Get請求,hello.....");      return "hello";  }  

@GetMapping是一個組合註解,是@RequestMapping(method = RequestMethod.GET)的縮寫。
所以我們可以將以上程式碼簡單的寫成:

@GetMapping("/rest")  public String restGet(){      System.out.println("Get請求,hello.....");      return "hello";  }  

二:@PostMapping請求

    @PostMapping("/rest")      public String restPost(){          System.out.println("Post請求,hello.....");          return "hello";      }  

三:@DeleteMapping請求

    @DeleteMapping("/rest")      public String restDelete(){          System.out.println("Delete請求,hello.....");          return "redirect:rest";      }  

四:@PutMapping請求

    @PutMapping("/rest")      public String restPut(){          System.out.println("Put請求,hello.....");          return "redirect:rest";      }  

叄:問題

一:post請求轉換為delete與put請求

由於form表單只支援GET和POST請求,而不支援DELETE和PUT等請求方式,所以我們實現delete和put請求往往會報錯找不到方法。

Spring提供了一個過濾器HiddenHttpMethodFilter,可以將DELETE和PUT請求轉換為標準的HTTP方式,即能將POST請求轉為DELETE或PUT請求。

具體實現:在web.xml文件中配置過濾器HiddenHttpMethodFilter

<!--使用Rest風格的URI,將頁面普通的post請求轉為delete或者put請求-->    <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>  

注意:delete和put請求最好使用Redirect(重定向),不然會報404錯誤。

二:解決請求亂碼問題

 <!--  解決亂碼問題-->    <filter>      <filter-name>CharacterEncodingFilter</filter-name>      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      <init-param>        <param-name>encoding</param-name>        <param-value>UTF-8</param-value>      </init-param>      <init-param>        <param-name>forceEncoding</param-name>        <param-value>true</param-value>      </init-param>    </filter>    <filter-mapping>      <filter-name>CharacterEncodingFilter</filter-name>      <url-pattern>/*</url-pattern>    </filter-mapping>  

注意:編碼的過濾器應該放在所有的過濾器前,否則不生效

肆:RequestMapping註解的屬性

屬性名 類型 描述
name String 可選屬性,用於為映射地址指定別名。
value String[] 可選屬性,同時也是默認屬性,用於映射一-個請求和一種方法,可以標註在一-個方法或一個類上。
method RequestMethod[] 可選屬性,用於指定該方法用於處理哪種類型的請求方式,其請求方式包括GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE和TRACE例如method=RequestMethod.GET表示只支援GET請求,如果需要支援多個請求方式則需要通過{}寫成數組的形式,並且多個請求方式之間是有英文逗號分隔。
params String[] 可選屬性,用於指定Request中必須包含某些參數的值,.才可以通過其標註的方法處理。
headers String[] 可選屬性,用於指定Request中必須包含某些指定的header的值,才可以通過其標註的方法處理。。
consumes String[] 可選屬性,用於指定處理請求的提交內容類型(Content-type),比如application/json,text/html等。
produces String[] 可選屬性,用於指定返回的內容類型,返回的內容類型必,須是request請求頭(Accept)中所包含的類型。

表中所有屬性都是可選的,但其默認屬性是value。當value是其唯一屬性時, 可以省略屬性名。
例如,下面兩種標註的含義相同:
@RequestMapping(value="/rest")
@RequestMapping("/rest")

伍:測試

新建index.html文件,加入以下程式碼。新建hello.html,用於請求後的頁面跳轉。

<h2>各種請求</h2>  <a href="rest">hello Get請求</a>  <form action="rest" method="post">      <button type="submit">hello Post請求</button>  </form>  <form action="rest" method="post">      <input type="hidden" name="_method" value="delete">      <button type="submit">hello Delete請求</button>  </form>  <form action="rest" method="post">      <input type="hidden" name="_method" value="put">      <button type="submit">hello put請求</button>  </form>  

結果: