SpringMVC study notes -11 RestFul style

RestFul is a style of resource positioning and resource operation. It is neither a standard nor a protocol. Software based on this style

Can be more concise, more hierarchical, and easier to implement the caching mechanism.

The traditional way of operating the Internet is achieved through different parameters, and the request methods are post and get. For different requests, we need to use different request paths:

localhost:8080/item/query?id=1
localhost:8080/item/add
localhost:8080/item/update
localhost:8080/item/delete?id=1

Using the RestFul style, we can keep the request path unchanged and call different methods through different request methods to achieve different functions:

localhost:8080/item/1   GET请求
localhost:8080/item     POST请求
localhost:8080/item     PUT请求
localhost:8080/item/1   DELETE请求

First look at the traditional request method: uri? parameter 1=value 1¶meter 2=value 2...

package com.zzt.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RestFulController {
    @RequestMapping("/add")
    public String test(int a, int b, Model model){
        model.addAttribute("msg",a+b);
        return "test";
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${msg}
</body>
</html>

@PathVariable annotation: used to implement RestFul style

package com.zzt.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RestFulController {
    @RequestMapping("/add/{a}/{b}")
    public String test(@PathVariable int a,@PathVariable int b, Model model){
        model.addAttribute("msg",a+b);
        return "test";
    }
}

Take the above code as an example, @PathVariable specifies that the value of parameter -int a comes from {a} in uri, and the value of parameter -int b comes from {b} in uri

At this time, it will be wrong to access in the traditional way, and you must use the RestFul style to receive parameters:

Remember we mentioned that @RequestMapping can set the request method? Yes, it can be combined with this attribute. (Of course, SpringMVC also provides sub-annotations, @GetMapping, @PostMapping, etc.).

    @GetMapping("/add/{a}/{b}")
    public String testGet(@PathVariable int a,@PathVariable int b, Model model){
        model.addAttribute("msg","get请求到达 : " + (a + b));
        return "test";
    }

    @PostMapping("/add/{a}/{b}")
    public String testPost(@PathVariable int a,@PathVariable int b, Model model){
        model.addAttribute("msg","post请求到达 : "+ (a+b));
        return "test";
    }
<body>
    <a href="add/1/2">get提交方式
    <form action="add/3/4" method="post">
        <input type="submit" value="提交">
    </form>
</body>

Advantages of RestFul: concise (eliminate the problem of using parameter names), efficient (caching mechanism), and safe (because the request does not carry parameter names)

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_39304630/article/details/113094840