REST 風格的請求方式

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

REST 風格的請求方式分別對應瞭如下四種請求,這四種請求有分別對應了四種對資源的操做:java

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

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

PUT -----------> 更新資源架構

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

GET

GET 請求咱們都很熟悉了,好比地址欄直接訪問,超連接訪問等。jsp

咱們建立一個控制器用來接收 GET 請求: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/{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 地址中的參數。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 的解決方法 章節。

相關文章
相關標籤/搜索