REST 表现层状态转化

1、REST是什么?
1) REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用
  ① 资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。

  ② 表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格式、XML 格式、JSON 格式表现,甚至可以采用二进制格式。

  ③ 状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。

  ④ 具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:GET 用来获取资源,POST 用来添加资源,PUT 用来修改资源,DELETE 用来删除资源

2)HiddenHttpMethodFilter
  浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。

  -1) 为什么请求隐含参数名称必须叫做”_method”

  -2) hiddenHttpMethodFilter 的处理过程


2.如何发送PUT请求或DELETE请求?
 * ①.配置HiddenHttpMethodFilter
 * ②.需要发送POST请求
 * ③.需要在发送POST请求时携带一个 name="_method"的隐含域,值为PUT或DELETE

REST风格:请求路径一致,通过不同的请求方式确定被那个方法处理请求

1) 配置HiddenHttpMethodFilter过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC02</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
  
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
</web-app>

 

2)控制器代码

package com.atguigu.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="/testREST/{id}", method=RequestMethod.GET)
    public String getUserById(@PathVariable("id")Integer id) {
        System.out.println("GET,id="+id);
        return "success";
    }
    
    @RequestMapping(value="/testREST", method=RequestMethod.POST)
    public String insertUser() {
        System.out.println("POST");
        return "success";
    }
    
    @RequestMapping(value="/testREST", method=RequestMethod.PUT)
    public String updateUser() {
        System.out.println("PUT");
        return "success";
    }
    
    @RequestMapping(value="/testREST/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable("id") Integer id) {
        System.out.println("DELETE,id="+id);
        return "success";
    }
    
    @RequestMapping(value="testAjax_DELETE", method=RequestMethod.DELETE)
    public void testAjax_DELETE(Integer id) {
        System.out.println("testAjax_DELETE,id="+id);
    }
    
}

3)请求链接

<%@ 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>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    function test() {
        $.ajax({
            url:"testAjax_DELETE",
            type:"DELETE",
            data:{id:1001},
            dataType:"json",
            success:function (obj){
                alert(obj);
            }
        });
    }
</script>
</head>
<body>

    <a href="testREST/1001">测试GET</a>
    <br />
    <form action="testREST" method="POST">
        <input type="submit" value="测试POST" />
    </form>
    <br />
    <form action="testREST" method="POST">
        <input type="hidden" name="_method" value="PUT" />
        <input type="submit" value="测试PUT" />
    </form>
    <br />
    <form action="testREST/1001" method="POST">
        <input type="hidden" name="_method" value="DELETE" />
        <input type="submit" value="测试DELETE" />
    </form>
    
    <input type="button" value="测试AJAX" onclick="test()" />

</body>
</html>

 

posted @ 2020-05-20 22:02  kkzhang  阅读(489)  评论(0编辑  收藏  举报