REST

Speaking of RESTful, let’s start with REST.

REST, or Representational State Transfer, is a design principle.

There is one subject missing from a clear description of REST, which is the presentation layer state transition of a resource. Resource Representational State Transfer.

Resource: Abstract all things in the network into resources, each Resource has a unique location identifier URL, such as a picture, a song, a video, a page…

Representational: The resource is presented in a form known as a representation layer. For example, show a picture, play a song, play a video, show a page…

State Transfer: After the “resource” is presented, the user will produce a certain State operation on the “resource”, which is bound to change the State of the “resource”. This process is called State Transfer.

The principle of REST

  1. Replace traditional urls with REST urls.

    Tradition URL is: http://localhost:9000/project/user/findOne? Id = 1, this URL is actually consists of two parts, the first part is the ontology URL: http://localhost:9000/project/user/findOne, the second part is the URL of the binding parameters: id = 1, use two parts of “?” Attached to it. This kind of locating way is considered a URL and parameters is not a whole, the real URL: http://localhost:9000/project/user/findOne can’t define a unique resource in the network. This is somewhat contrary to the original standard of URL, which stipulated that “?” cannot be used in URL at the beginning of its formulation. Connection parameters.

    The URL represented by REST must be a unique path and cannot be separated. Using REST according to the above URL is: http://localhost:9000/project/user/findOne/1/.

  2. The four verbs correspond to the four operations on the server side (CRUD, add, delete, change, search).

    In the absence of REST, we usually use GET and POST to communicate back and forth. GET and POST build urls that map to database operations such as SELECT, UPDATE, INSERT, and DELETE. The request initiated by the front end only knows which OPERATION in CRUD the URL corresponds to when it actually executes SQL in the database. In terms of the front end presentation layer, we do not know which operation in CRUD the URL corresponds to. These days, urls are usually made up of specific English words, such as /user/save, so we can guess that this is an INSERT operation. In this case, the COUPLING degree between URL and encoding is relatively high.

    REST provides four HTTP request verbs that represent four request modes: GET, POST, PUT, and DELETE. REST uses these verbs to represent four operations on a server-side database when it expects communication between the front and back ends.

    A GET request corresponds to an INSERT, a POST request corresponds to an UPDATE/INSERT, a PUT request corresponds to an INSERT/update, and a DELETE request corresponds to a DELETE.

    Currently, mainstream browsers only support GET and POST HTTP communication modes, but many Java third-party frameworks (SpringMVC, SpringFlux) support REST interface communication. In this case, before communicating with the browser, Generally, the Httpclient or RPC client is used to convert the REST communication mode into the GET and POST modes that the browser can communicate with.

  3. Service interfaces typically return data in JSON or XML format.

  4. For all REST-designed interfaces, invocation must be requested as specified by the design. For example, if the interface is PUT, it must be called in PUT mode.

RESTful

RESTful is a software architecture style, and an architecture can be called RESTful if its design follows REST design principles. Elasticsearch is RESTful architecture as described in this series of articles.

RESTful implementation in SpringBoot

1. GET the interface

@RestController
@RequestMapping("user")
public class UserController {

    @GetMapping("findOne/{id}/{username}/")
    public String findOne(@PathVariable("id") String id, @PathVariable("username") String username) {
        System.out.println("id:" + id);
        System.out.println("username:" + username);
        return "ok"; }}Copy the code

2. POST/PUT interface

Same as the original form.

@RestController
@RequestMapping("user")
public class UserController {

    @PutMapping("saveOne")
    public String saveOne(User user) {
        System.out.println("user:" + user);
        return "ok";
    }

    @PostMapping("updateOne")
    public String updateOne(User user) {
        System.out.println("user:" + user);
        return "ok"; }}Copy the code

3. DELETE the interface

@RestController
@RequestMapping("user")
public class UserController {

    @DeleteMapping("deleteOne/{id}/")
    public String deleteOne(@PathVariable("id") String id) {
        System.out.println("id:" + id);
        return "ok"; }}Copy the code