REST風格的網絡接口

REST即(資源)表現層狀態轉化(英文:(Resource)Representational State Transfer,簡稱REST)是Roy Fielding博士在2000年他的博士論文中提出來的一種軟件架構風格。它是一種針對網絡應用的設計和開發方式,能夠下降開發的複雜性,提升系統的可伸縮性。目前在三種主流的Web實現方案中,由於REST模式的Web服務與複雜的SOAP和XML-RPC對比來說明顯的更加簡潔,愈來愈多的web服務開始採用REST風格設計和實現。例如,Amazon.com提供接近REST風格的Web服務進行圖書查找;雅虎提供的Web服務也是REST風格的。html

資源(Resource):任何事物,只要有被引用到的必要,它就是一個資源。資源能夠是實體,也能夠是一個抽象概念。能夠用URI(統一資源定位符)指向它,每種資源對應一個特定的URI。要獲取這個資源,訪問它的URI便可,所以URI及爲每個資源的獨一無二的識別符。java

表現層(Representation):把資源具體呈現出來的形式,叫作它的表現層。好比:文本能夠用txt格式表現,也能夠用HTML格式、XML格式、JSON格式表現,甚至能夠採用二進制格式web

狀態轉化(State Transfer):每發出一個請求,就表明了客戶端和服務器端的一次交互過程。HTTP協議,是一個無狀態協議,即全部的狀態都保存在服務器端。所以,若是客戶端想要操做服務器,必須經過某種手段讓服務器端發生「狀態轉化」。這種轉化是創建在表現層之上的,因此就是「表現層狀態轉化」。spring

遵照REST體系結構約束的Web服務API稱爲RESTful API。基於HTTP的RESTful API定義有如下幾個方面:瀏覽器

RESTful能夠經過一套統一的接口爲Web、iOS、Android提供服務。服務器

以CRUD爲例:網絡

咱們以前的方法請求多是這樣的:架構

news?id=1                    獲得id=1的newsmvc

newsDelete?id=1          刪除id=1的newsapp

newsUpdate?id=1        修改id=1的news

newsAdd                        添加news

而REST風格的API增刪改查都是一個地址,具體操做靠HTTP頭部信息判斷:

*/news/1    HTTP GET:獲得id=1的news

*/news/1    HTTP DELETE:刪除id=1的news

*/news/1    HTTP PUT:更新id=1的news

*/news    HTTP POST:新增news

瀏覽器form表單只支持GET和POST請求,Spring3.0以後添加了一個過濾器(HiddenHttpMethodFiler),能夠將這些請求轉化爲標準的HTTP方法,使得支持GET、POST、PUT與DELETE請求

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	
	<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:能夠把POST請求轉爲DELETE或POST請求 -->
	<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>
	
	<!-- 配置前置控制器 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- DispatcherServlet的一個初始化參數:配置SpringMVC 配置文件的位置和名稱 -->
		<!-- 實際上也能夠不經過contextConfigLocation來配置SpringMVC的配置文件,而使用默認的 默認的配置文件爲:/WEB-INF/<servlet-name>-servlet.xml 
			springDispatcherServlet-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

SpringMVCTest.java

package com.f145a.springmvc.handlers;

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;;

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
	
	private static final String SUCCESS="success";
	
	
	/**
	 * REST風格的URL
	 * 以CRUD爲例:
	 * 新增:/order POST
	 * 修改:/order/1 PUT			update?id=1
	 * 獲取:/order/1 GET			get?id=1
	 * 刪除:/oeder/1 DELETE		delete?id=1
	 * 
	 * 如何發送PUT請求和DELETE請求呢?
	 * 1.須要配置HiddenHTTPMethodFilter
	 * 2.須要發送POST請求
	 * 3.須要在發送POST請求時攜帶一個name="_method"的隱藏域,值爲DELETE或PUT
	 * 
	 * 在SpringMVC的目標方法中如何獲得id呢?
	 * 使用@PathVariable註解
	 */
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
	public String testRestPut(@PathVariable Integer id){
		System.out.println("testRest PUT:"+id);
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
	public String testRestDelete(@PathVariable Integer id){
		System.out.println("testRest Delete:"+id);
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest",method=RequestMethod.POST)
	public String testRest(){
		System.out.println("testRest POST");
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
	public String testRest(@PathVariable Integer id){
		System.out.println("testRest GET:"+id);
		return SUCCESS;
	}
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="springmvc/testRest/1" method="post">
	<input type="hidden" name="_method" value="PUT">
	<input type="submit" value="TestRest PUT">
</form>

<form action="springmvc/testRest/1" method="post">
	<input type="hidden" name="_method" value="DELETE">
	<input type="submit" value="TestRest DELETE">
</form>

<form action="springmvc/testRest" method="post">
	<input type="submit" value="TestRest POST">
</form>

<a href="springmvc/testRest/1">TestRest GET</a>

</body>
</html>

歡迎訪問個人我的博客http://www.chengzequn.top

相關文章
相關標籤/搜索