REST 風格的請求方式


REST:即 Representational State Transfer。(資源)表現層狀態轉化。是目前最流行的一種互聯網軟件架構。它結構清晰、符合標准、易於理解、擴展方便, 所以正得到越來越多網站的采用。使用 REST 風格的請求方式,可以簡化 url,達到使用同一個 url 不同請求方式來執行不同的方法。

REST 風格的請求方式分別對應了以下四種請求,這四種請求有分別對應了四種對資源的操作:

GET -----------> 獲取資源

POST ---------> 新建資源

PUT -----------> 更新資源

DELETE ------> 刪除資源

GET

GET 請求我們都很熟悉了,比如地址欄直接訪問,超鏈接訪問等。

我們創建一個控制器用來接收 GET 請求:

package com.pudding.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestController {

	@RequestMapping(value = "/rest/{id}", method = RequestMethod.GET)
	public String get(@PathVariable Integer id) {
		System.out.println("GET --- 查詢數據 --- " + id);
		return "success";
	}

}

並且使用一個超鏈接來發出 GET 請求:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rest風格請求</title>
</head>
<body>
	<a href="rest/1">GET 請求</a>
</body>
</html>

訪問頁面,點擊頁面上的超鏈接,會發現畫面跳轉到了 success.jsp 並且在控制台上輸出了 "GET --- 查詢數據 --- 1" 。說明控制器成功的接收到了 GET 請求並且獲取到了 url 地址中的參數。

POST

POST 請求最常見的方式就是 form 表單了。

我們創建一個控制器來接收 POST 請求:

package com.pudding.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestController {

	@RequestMapping(value = "/rest", method = RequestMethod.POST)
	public String post() {
		// 接收表單中的各種信息
		System.out.println("POST --- 創建數據");
		return "success";
	}

}

並且使用一個 form 表單來發出 POST 請求:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="rest" method="post">
		<input type="submit" value="POST 請求" />
	</form>
</body>
</html>

訪問頁面,點擊頁面上表單中的按鈕,會發現畫面跳轉到了 success.jsp 並且在控制台上輸出了 "POST --- 創建數據" 。說明控制器成功的接收到了 GET 請求。

PUT 和 DELETE

創建 GET 請求和創建 PUT 請求都很簡單,但是 PUT 請求和 DELETE 請求呢?在我們正常的訪問當中是創建不出 PUT 請求和 DELETE 請求的,所以我們需要使用 Spring 三大組件之一的過濾器Filter來發出 PUT 請求和 DELTE 請求。

發送 PUT 和 DELTE 請求的步驟:

  1. 在 web.xml 中添加HiddenHttpMethodFilter過濾器:
	<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. 創建一個帶 POST 請求的 form 表單:
	<form action="rest" method="post">
		<input type="submit" value="PUT 請求" />
	</form>
  1. 在表單內添加 name 為 _method 的標簽:
	<form action="rest" method="post">
		<input type="hidden" name="_method" value="put" />
		<input type="submit" value="PUT 請求" />
	</form>

DELTE 請求同理,只需要將 put 求改為 delte 即可。不區分大小寫。

如果按照以上步驟處理完成之后,點擊按鈕發現 HTTP 405 的錯誤提示:"消息 JSP 只允許 GET、POST 或 HEAD。Jasper 還允許 OPTIONS"。那么請參 HTTP 405 的錯誤提示:消息 JSP 只允許 GET、POST 或 HEAD。Jasper 還允許 OPTIONS 的解決方法 章節。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM