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>

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/85237292