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

概述

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

你能get的知識點

一、什麼是Rest風格?
二、基於springmvc實現REST風格的四種請求方式
三、post請求轉換爲deleteput請求
四、解決請求亂碼問題
五、RequestMapping註解的屬性
@java


壹:rest風格

一:什麼是Rest風格?

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

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

二: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等來區分使用者的行爲操做,而是使用不一樣的請求方式來描述行爲。json

貳:基於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)的縮寫。
因此咱們能夠將以上代碼簡單的寫成:mvc

@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請求每每會報錯找不到方法。app

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>

結果:

相關文章
相關標籤/搜索