Interface architecture style - RESTful

Interface architecture style - RESTful

The interface here refers to API (application program interface)

API (Application Programming Interface) refers to some pre-defined interfaces (such as functions and HTTP interfaces) or conventions for the connection of different components of the software system. It is used to provide a set of routines that can be accessed by applications and developers based on some software or hardware without accessing the source code or understanding the details of the internal working mechanism.

Architecture style: simply speaking, it is the organization (appearance) of api
For example: http://localhost:8081/mytrans/addStudent?name=lisi&age=26

RESTful architecture style

What is REST

The Chinese name is "presentation layer state transition". It is not a standard, but a style of software architecture design. It only puts forward the architecture concept and design principle of group client and server interaction. Based on this concept and principle, the time interface can be more concise and hierarchical
For example, we access an HTTP interface: http://localhost:8080/boot/order?id=1021&status=1
The RESTful style is adopted. The address is: http://localhost:8080/boot/order/1021/1

Presentation layer state transition:
Presentation layer: it is the view layer, which displays resources, and displays the results of operating resources through view pages, etc
State: refers to the transformation of resources
Transfer: resources can be changed, such as adding new students, that is, the status of new, deleting and other http actions to change resources, that is, transfer

Elements in REST:
REST is used to represent resources and operations on resources. In the Internet, resources or operations are represented by url

Operations on resources:

  • Query resources: look, find resources through URLs
  • Updating resources: updating, editing
  • Deleting resources: removing
  • New resources: adding

Resources are represented by URL s, and actions in http (request mode) are used to represent operations on resources (CRUD)

  • GET: query resources - sql select
    Query individual students:
    http://localhost:8080/myboot/student/1001
    http://localhost:8080/myboot/student/1001/1
    Query multiple students:
    http://localhost:8080/myboot/students/1001/1002
  • POST: create resource - sql insert
    http://localhost:8080/myboot/student
    Passing parameters in a post request
<form action="http://localhost:8080/myboot/student" method="post">
	full name : <input type="text" name="name" />
	Age : <input type="text" name="age" />
</form>
  • PUT: update resource - sql updata
    Update resources for student 1
<form action="http://localhost:8080/myboot/student/1" method="post">
	full name : <input type="text" name="name" />
	Age : <input type="text" name="age" />
	<input type="hiden" name="_method" value="PUT" />
</form>
  • DELETE: DELETE resources - sql delete
<a herf="http://Localhost: 8080 / myboot / student / 1 "> delete</a>

For browsers, GET and POST are supported by default, and PUT and DELETE are not supported. However, we can indirectly support them in other ways, that is, use POST to simulate PUT and DELETE

Irrelevant information such as paging and sorting parameters can still be placed after the url, for example:
http://localhost:8080/myboot/students?page=1&pageSize=20

One sentence means REST: use url to represent resources and http action to operate resources

RESTful annotation

Annotation nameAnnotation meaning
@PathVariableGet data from url
@GetMappingget requests are supported, which is equivalent to @ RequestMapping(method=RequestMethod.GET)
@PostMappingSupport post request, which is equivalent to @ RequestMapping(method=RequestMethod.POST)
@PutMappingSupport put request, equivalent to @ RequestMapping(method=RequestMethod.PUT)
@DeleteMappingdelete request is supported, which is equivalent to @ RequestMapping(method=RequestMethod.DELETE)
@RestControllerConforming to the annotation, it is a combination of @ Controller and @ ResponseBody, indicating that all methods in the current class have joined @ ResponseBody

The following is a controller to test:

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

@RestController
public class MyRestController {

    // Use of learning annotations

    // Query students with id=1001

    /**
     * @PathVarable(Path variable): get the data in the url
     *          Properties:
     *              value : The path variable name is defined in front of the controller method parameter and can be omitted
     *                      PathVariable(value = "Variable name ") is equivalent to PathVariable(" variable name ")
     */
    /*
        For example, the url is http://localhost:8081/myboot/student/1002
        Here {stuId} is 1002, and 1002 is assigned to the method parameter studentId
     */
    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable(value = "stuId") Integer studentId) {

        return "Query students id=" + studentId;
    }

    /**
     * Create a Post request for the resource
     */
    @PostMapping("/student/{name}/{age}")
    public String createStudent(@PathVariable("name") String name,
                                @PathVariable("age") Integer age) {

        return "Created with the name" + name + "Age is" + age + "Student object";
    }

    /**
     * Update resources
     * When the name of the path variable is the same as the formal parameter name, the contents after the PathVariable can not be written
     */
    @PutMapping("/student/{id}/{age}")
    public String modifyStudent(@PathVariable Integer id,
                                @PathVariable Integer age) {

        return "Update to id=" + id + "age=" + age;
    }


    /**
     * Delete resource
     */
    @DeleteMapping("/student/{id}")
    public String removeStudent(@PathVariable Integer id) {

        return "Deleted id by" + id + "Students";
    }
}

Put and delete requests are supported in pages or ajax

There is a filter in spring MVC that supports the conversion of post requests to put and delete requests
Use filters: org springframework. web. filter. HiddenHTTPMethodFilter
Function: convert the post in the request into put and delete requests

Implementation steps:

  1. application. Enable the use of HiddenHTTPMethodFilter filter in the properties file
#Enable delete that supports put
spring.mvc.hiddenmethod.filter.enabled=true
  1. The requested page needs to contain_ method parameter, whose value is put or delete, but the request is initiated in post mode
    <form action="student/test" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="submit" value="Test request mode">
    </form>

Finally, it is emphasized that the REST style must be unique, that is, the request method + url is unique

Keywords: Spring Boot Back-end architecture RESTful SSM

Added by ryza_ on Sat, 11 Dec 2021 12:17:25 +0200