[spring] The difference between REST and RESTful and the basic implementation


Introduction

REST concept:

REST: full name: Representational State Transfer, meaning: presentation layer resource state transfer

The detailed explanation can be divided into three parts:

  • resource

A resource is a way of looking at a server, that is, seeing a server as consisting of many discrete resources. Each resource is a nameable abstraction on the server. Because a resource is an abstract concept, it can not only represent a file in the server file system, a table in the database, etc., but can design resources as abstract as possible, as long as the imagination allows And client application developers can understand. Similar to object-oriented design, resources are organized around nouns, with nouns being the first focus. A resource can be identified by one or more URIs. A URI is both the name of a resource and its address on the Web. Client applications interested in a resource can interact with it through the resource's URI.

  • representation of resources

A resource representation is a description of the state of the resource at a particular moment. Can be transferred (exchanged) between client-server. The representation of resources can be in various formats, such as HTML/XML/JSON/plain text/image/video/audio and so on. The representation format of resources can be determined through a negotiation mechanism. The representation of the request-response direction usually uses a different format.

  • state transition

State transfer is about transferring representations representing the state of a resource between the client and the server. By transferring and manipulating the representation of resources, the purpose of manipulating resources is achieved indirectly.

REST makes good use of some of the features of HTTP itself, such as HTTP verbs, HTTP status codes, HTTP headers, etc.

The main principles of REST architecture:

  • There is a resource identifier for all resources on the network;
  • Operations on the resource do not change the identifier;
  • The same resource has multiple representations (xml, json);
  • All operations are stateless;

An architectural approach that conforms to the above REST principles is called RESTful;

RESTful concept:

RESTful是一种常见的REST应用,是遵循REST风格的web服务,REST式的(rest与restful相比,多了一个ful,就英语层面来说是一个形容词,restful翻译为中文为: “rest式的”)web服务是一种ROA(面向资源的架构)

REST和RESTful的联系与区别:

restful是由rest派生出来的。

RESTful实现

具体说,就是http协议里面,四个表示操作方式的动词:get,post,put,delete,它们分别对应四种基本操作:get用来获取资源,post用来新建资源,put用来更新资源,delete用来删除资源

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

在这里插入图片描述

GET, 查询所有用户信息

	/**
	* 使用RESTFul模拟用户资源的增删改查
	* /user    GET     查询所有用户信息
	*/
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String getAllUser(){
    
    
        System.out.println("查询所有用户信息");
        return "success";
    }
<a th:href="@{/user}">查询所有用户信息</a><br>

GET, 根据用户ID查询用户信息

	/**
	* 使用RESTFul模拟用户资源的增删改查
	* /user     GET     根据用户id查询用户信息
	*/
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public String getUserById(){
    
    
        System.out.println("根据id查询用户信息");
        return "success";
    }
<a th:href="@{/user/1}">根据id查询用户信息</a><br>

POST, 添加用户信息

	/**
	* 使用RESTFul模拟用户资源的增删改查
	* /user     POST     添加用户信息
	*/
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public String insertUser(String username, String password){
    
    
        System.out.println("添加用户信息:"+username+","+password);
        return "success";
    }
<form th:action="@{/user}" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="添加"><br>
</form>

PUT, 修改用户信息

	/**
	* 使用RESTFul模拟用户资源的增删改查
	* /user     PUT     修改用户信息
	*/
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public String updateUser(String username, String password){
    
    
        System.out.println("修改用户信息:"+username+","+password);
        return "success";
    }
<form th:action="@{/user}" method="post">
    <input type="hidden" name="_method" value="PUT">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="修改"><br>
</form>

注意:

由于浏览器只支持发送get和post方式的请求,那么该如何发送put和delete请求呢?

SpringMVC 提供了 HiddenHttpMethodFilter 帮助我们将 POST 请求转换为 DELETE 或 PUT 请求

HiddenHttpMethodFilter 处理put和delete请求的条件: a>当前请求的请求方式必须为post , b>当前请求必须传输请求参数_method 满足以上条件,HiddenHttpMethodFilter 过滤器就会将当前请求的请求方式转换为请求参数_method的值,因此请求参数_method的值才是最终的请求方式

配置HiddenHttpMethodFilter,在web.xml中

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

注:

目前为止,SpringMVC中提供了两个过滤器:CharacterEncodingFilter(编码过滤器)和HiddenHttpMethodFilter(请求方式过滤器),在web.xml中注册时,必须先注册CharacterEncodingFilter,再注册HiddenHttpMethodFilter。原因:

在 CharacterEncodingFilter 中通过 request.setCharacterEncoding(encoding) 方法设置字符集的request.setCharacterEncoding(encoding) 方法要求前面不能有任何获取请求参数的操作,而 HiddenHttpMethodFilter 恰恰有一个获取请求方式的操作:

String paramValue = request.getParameter(this.methodParam);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324098164&siteId=291194637