天天看點

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

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

概述

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

你能get的知識點

1、什麼是Rest風格?

2、基于springmvc實作REST風格的四種請求方式

3、post請求轉換為delete與put請求

4、解決請求亂碼問題

5、RequestMapping注解的屬性

@

目錄

壹:rest風格

一:什麼是Rest風格?

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

貳:基于springmvc實作REST風格的四種請求方式

一:@GetMapping請求

二:@PostMapping請求

三:@DeleteMapping請求

四:@PutMapping請求

叁:問題

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

二:解決請求亂碼問題

肆:RequestMapping注解的屬性

伍:測試

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

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

請求 說明 用于 例子 例子說明

@GetMapping 比對GET方式的請求; 一般用于讀取資料 /user/1 擷取一号使用者資訊

@PostMapping 比對POST方式的請求; 一般用于新增資料 /user/1 新增一号使用者

@PutMapping 比對PUT方式的請求; 一般用于更新資料 /user/1 修改一号使用者

@DeleteMapping 比對DELETE方式的請求; 一般用于删除資料 /user/1 删除一号使用者

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

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為例,該組合注解是@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")

System.out.println("Get請求,hello.....");
return "hello";           
@PostMapping("/rest")
public String restPost(){
    System.out.println("Post請求,hello.....");
    return "hello";
}           
@DeleteMapping("/rest")
public String restDelete(){
    System.out.println("Delete請求,hello.....");
    return "redirect:rest";
}           
@PutMapping("/rest")
public String restPut(){
    System.out.println("Put請求,hello.....");
    return "redirect:rest";
}           

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

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

具體實作:在web.xml檔案中配置過濾器HiddenHttpMethodFilter

<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>           
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>           

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

<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-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>           

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

屬性名 類型 描述

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,用于請求後的頁面跳轉。

各種請求

hello Get請求
<button type="submit">hello Post請求</button>           
<input type="hidden" name="_method" value="delete">
<button type="submit">hello Delete請求</button>           
<input type="hidden" name="_method" value="put">
<button type="submit">hello put請求</button>           

結果:

原文位址

https://www.cnblogs.com/lomtom/p/12591836.html