1. 程式人生 > >SpringMVC_4_REST的GET(得到資料)、POST(新增資料)、DELETE(刪除資料)、PUT(更新資料)操作

SpringMVC_4_REST的GET(得到資料)、POST(新增資料)、DELETE(刪除資料)、PUT(更新資料)操作

REST

  • REST:即 Representational State Transfer。**(資源)表現層狀態轉化。是目前最流行的一種網際網路軟體架構。**它結構清晰、符合標準、易於理解、擴充套件方便、所以正得到越來越多網站的採用

  • 資源(Resources):**網路上的一個實體,或者說是網路上的一個具體資訊。**它可以是一段文字、一張圖片、一首歌曲、一種服務,總之就是一個具體的存在。可以用一個URI(統一資源定位符)指向它,每種資源對應一個特定的URI。要獲取這個資源,訪問它的URL就可以,因此URI即為每一個資源的獨一無二的識別符號。

    tips:

    URI 屬於 URL 更高層次的抽象,一種字串文字標準。

    就是說,URI 屬於父類,而 URL 屬於 URI 的子類。URL 是 URI 的一個子集。

  • 表現層(Representation):把資源具體呈現出來的形式,叫做它的表現層(Representation)。比如,文字可以用txt格式表現,也可以用HTML格式、XML格式、JSON格式表現,甚至可以採用二進位制格式。

  • 狀態轉化(State Transfer):每發出一個請求,就代表了客戶端和伺服器的一次互動過程。Http協議,是一個無狀態協議,即所有的狀態都儲存在伺服器端。因此,如果客戶端想要操作伺服器,必須通過某種手段,讓伺服器發生"狀態轉化"。而這種轉化是建立在表現層之上的,所以就是“表現層狀態轉化

    。具體說,就是HTTP協議裡面,四個表示操作方式的動詞:GET、POST、PUT、DELETE。它們分別對用四種基本操作:GET用來獲取資源,POST用來新建資源,PUT用來更新資源,DELETE用來刪除資源。

tip:

這裡寫下遇到的一個問題,我用使用PUT,DELETE操作時頁面始終405,找不到相應的method,只允許GET,POST,HEAD。

對比發現,我用的是TomCat9,教學視訊用的是TomCat7

可解決的方法:將TomCat9更換為TomCat7.

  • 示例:

    • 前提:在web.xml中配置好一個 HiddenHttpMethidFilter (可以把POST請求轉為DELETE或POST請求)
    <!--配置 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>
    
    • /order/1 HTTP GET : 得到 id = 1 的order
      在這裡插入圖片描述
      在這裡插入圖片描述

    • /order/1 HTTP DELETE: 刪除 id = 1 的order

在這裡插入圖片描述

在這裡插入圖片描述

  • /order/1 HTTP PUT: 更新id = 1 的order

在這裡插入圖片描述
在這裡插入圖片描述

  • /order HTTP POST: 新增id = 1 的order

在這裡插入圖片描述
在這裡插入圖片描述

  • HiddenHttpMethodFilter:瀏覽器form表單只支援GET與POST請求,而DELETE、PUT等method並不支援,Spring3.0添加了一個過濾器,可以將這些請求轉換為標準的http方法,使得支援GET、POST、PUT、DELETE請求。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--配置 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>


    <!--配置 DispatcherServlet-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置DispatcherServlet的一個初始化引數:配置SrpingMVC 配置檔案的位置和名稱-->
        <!--實際上也可以不通過contextConfigLocation 來配置SpringMVC 的配置檔案,而使用預設的。
            預設的配置檔案為: /WEB-INF/<servlet-name>-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>

springDispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!--配置自定掃描的包-->
    <context:component-scan base-package="com.springmvc.handlers"></context:component-scan>

    <!--配置檢視解析器: 如何把handler方法返回值解析為實際的物理檢視-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>