RESTful的介绍和简单实现

RESTful的介绍和简单实现

1.REST:Representational State Transfer,表现层资源状态转移。

资源:
资源是一种看待服务器的方式,即,将服务器看作是由很多离散的资源组成。每个资源是服务器上一个可命名的抽象概念。因为资源是一个抽象的概念,所以它不仅仅能代表服务器文件系统中的一个文件、数据库中的一张表等等具体的东西,可以将资源设计的要多抽象有多抽象,只要想象力允许而且客户端应用开发者能够理解。与面向对象设计类似,资源是以名词为核心来组织的,首先关注的是名词。一个资源可以由一个或多个URI来标识。URI既是资源的名称,也是资源在Web上的地址。对某个资源感兴趣的客户端应用,可以通过资源的URI与其进行交互。

资源的表述:
资源的表述是一段对于资源在某个特定时刻的状态的描述。可以在客户端-服务器端之间转移(交换)。资源的表述可以有多种格式,例如HTML/XML/JSON/纯文本/图片/视频/音频等等。资源的表述格式可以通过协商机制来确定。请求-响应方向的表述通常使用不同的格式。

状态转移:
状态转移说的是:在客户端和服务器端之间转移(transfer)代表资源状态的表述。通过转移和操作资源的表述,来间接实现操作资源的目的。

2.RESTful风格的使用使原本的参数也成为了路径的一部分

REST 风格提倡 URL 地址使用统一的风格设计,从前到后各个单词使用斜杠分开,不使用问号键值对方式携带请求参数,而是将要发送给服务器的数据作为 URL 地址的一部分,以保证整体风格的一致性。

3.RESTful的实现,

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

注意:浏览器只能发送get和post请求若要发送put和delete请求,需要在web.xml中配置一个过滤器HiddenHttpMethodFilter配置了过滤器之后,发送的请求要满足两个条件,才能将请求方式转换为put或delete
1.当前请求必须为post
2.当前请求必须传输请求参数_method,_method的值才是最终的请求方式

4.代码实现
<?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">
    <!--设置处理请求方式的过滤器-->
    <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>
</web-app>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>index.html</h1>
    <a th:href="@{/user}">查询所有的用户信息</a><br>
    <a th:href="@{/user/1}">查询id为1的用户信息</a><br>
    <form th:action="@{/user}" method="post">
        <input type="submit" value="添加用户信息">
    </form>

    <form th:action="@{/user}" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="submit" value="修改用户信息">
    </form>
    <form th:action="@{/user/5}" method="post">
        <input type="hidden" name="_method" value="delete">
        <input type="submit" value="删除用户信息">
    </form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功</title>
</head>
<body>
    <h1>success.html</h1>
</body>
</html>
package com.qcw.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author wqc
 * @date 2022/9/28 16:35
 *
 * 查询所有的用户信息    /user-->get
 * 根据id查询所有的信息  /user/1-->get
 * 添加用户信息   /user-->post
 * 修改用户信息   /user-->put
 * 删除用户信息   /user/1-->delete
 *
 * 注意:浏览器只能发送get和post请求
 *  若要发送put和delete请求,需要在web.xml中配置一个过滤器HiddenHttpMethodFilter
 *  配置了过滤器之后,发送的请求要满足两个条件,才能将请求方式转换为put或delete
 *   1.当前请求必须为post
 *   2.当前请求必须传输请求参数_method,_method的值才是最终的请求方式
 */
@Controller
public class TestRestController {
    
    

    //@RequestMapping(value = "/user",method = RequestMethod.GET)
    @GetMapping("/user")
    public String getAllUser(){
    
    
        System.out.println("查询所有的用户信息 /user-->get");
        return "success";
    }

    //@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable("id") Integer id){
    
    
        System.out.println("查询id为1的用户信息 /user/"+id+"-->get");
        return "success";
    }

    //@RequestMapping(value = "/user",method = RequestMethod.POST)
    @PostMapping(value = "/user")
    public String insertUser(){
    
    
        System.out.println("添加用户信息   /user-->post");
        return "success";
    }



    //@RequestMapping(value = "/user",method = RequestMethod.PUT)
    @PutMapping("/user")
    public String updateUser(){
    
    
        System.out.println("修改用户信息   /user-->put");
        return "success";
    }

    //@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
    
    
        System.out.println("删除用户信息   /user/"+id+"-->delete");
        return "success";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_47637405/article/details/127095594
今日推荐