03-SpringMVC_RESTful

1. REST style is what

  1. Resources (Resources): an entity on the network, or is a specific information on the network.
    It can be a piece of text, a picture, a song, a service, in short, is a concrete existence.
    It can be directed by a URI (Uniform Resource Locator), each corresponding to a particular resource URI.
    Get this resource, you can access its URI, so URI is the unique identifier for each resource.
  2. Presentation layer (Representation): The resources presented in the form of concrete, called its presentation layer (Representation). For example, you can use the text format txt performance, you can also use HTML format, XML format, JSON format performance, even in binary format.
  3. State transformation (State Transfer): Each issue a request on behalf of a client interaction and servers. HTTP protocol is a stateless protocol, i.e. all states are stored in the server. Therefore, if the client wants to operate the server, must by some means, let the server take place "state transformation" (State Transfer). And this transformation is based on the presentation layer, so is the "presentation layer state transformation."
  4. Specifically, there is the HTTP protocol, four verb indicates the operation mode: GET, POST, PUT, DELETE. Which correspond to the four basic operations: GET used to obtain resources, POST is used to create a new resource, PUT for updating resources, DELETE to delete the resource.

以简洁的URL地址提交请求, 以请求方式区分对资源的操作

2. URL example

Example:/资源名/资源标识符

/order/1  	HTTP GET :		得到 id = 1 的 order   
/order/1  	HTTP DELETE:	删除 id = 1的 order   
/order/1  	HTTP PUT:		更新id = 1的 order   
/order     	HTTP POST:		新增 order 

3. Experiment Code

3.1 filter configuration HiddenHttpMethodFilter

<!-- 支持REST风格的过滤器:可以将POST请求转换为PUT或DELETE请求 -->
<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>

3.2 Code

package com.lz.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;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName MyFirstController
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/

@Controller
@RequestMapping("/springmvc")
public class RESTController {

    @RequestMapping(value = "/testRESTGet/{id}", method = RequestMethod.GET)
    public String testRESTGet(@PathVariable("id") Integer id) {
        System.out.println("get id =" + id);
        return "success";
    }

    @RequestMapping(value = "/testRESTPost", method = RequestMethod.POST)
    public String testRESTPost() {
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/testRESTDelete/{id}", method = RequestMethod.DELETE)
    public String testRESTDelete(@PathVariable("id") Integer id) {
        System.out.println("delete id =" + id);
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/testRESTPut/{id}", method = RequestMethod.PUT)
    public String testRESTPut(@PathVariable("id") Integer id) {
        System.out.println("put id = " + id);
        return "success";
    }
}

3.3 Request link

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>

<html>
<body>
    <!-- 实验1 测试 REST风格 GET 请求 -->
    <a href="springmvc/testRESTGet/1">testREST GET</a><br/><br/>

    <!-- 实验2 测试 REST风格 POST 请求 -->
    <form action="springmvc/testRESTPost" method="POST">
        <input type="submit" value="testRESTPost">
    </form>

    <!-- 实验3 测试 REST风格 PUT 请求 -->
    <form action="springmvc/testRESTPut/1" method="POST">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="testRESTPut">
    </form>

    <form action="springmvc/testRESTDelete/1" method="POST">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="testRESTDelete">
    </form>
</body>
</html>

Published 42 original articles · won praise 0 · Views 488

Guess you like

Origin blog.csdn.net/qq_35199832/article/details/104254328