SpringBoot ~ Restful Request Style

Keywords: Java Database SQL less JSON

Restful Request Style

  • In RESTful architecture, each web site represents a resource, so there can be no verbs in the web site, only nouns, and the nouns used often correspond to the table names of the database. Generally speaking, the tables in the database are collection s of the same record, so the nouns in the API should also use the plural.
  • For the specific operation types of resources, they are represented by HTTP verbs.

    • There are five commonly used HTTP verbs (corresponding SQL commands in parentheses).

      • GET (SELECT): Extract resources (one or more) from the server.
      • POST (CREATE): Create a new resource on the server.
      • PUT (UPDATE): Update the resource on the server (the client provides the changed full resource).
      • PATCH (UPDATE): Update resources on the server (client provides changed attributes).
      • DELETE (DELETE): Delete resources from the server.
    • There are also two less commonly used HTTP verbs.

      • HEAD: Get metadata for resources.
      • OPTIONS: Get information about which attributes of resources the client can change.
  • Examples are as follows:

    • GET/zoos: List all zoos
    • POST/zoos: A New Zoo
    • GET/zoos/ID: Get information about a designated Zoo
    • PUT/zoos/ID: Update information about a designated zoo (provide all information about the zoo)
    • PATCH/zoos/ID: Update information about a designated zoo (provide some information about the zoo)
    • DELETE/zoos/ID: Delete a zoo
    • GET/zoos/ID/animals: List all animals in a designated Zoo
    • DELETE/zoos/ID/animals/ID: Delete a designated animal from a designated Zoo
  • Status code

    • 200 OK - [GET]: The server successfully returns the data requested by the user, which is Idempotent.
    • 201 CREATED - [POST/PUT/PATCH]: Users successfully created or modified data.
    • 202 Accepted - [*]: Indicates that a request has entered the background queue (asynchronous task)
    • 204 NO CONTENT - [DELETE]: User deleted data successfully.
    • 400 INVALID REQUEST - [POST/PUT/PATCH]: There is an error in the user's request, and the server does not perform the operation of creating or modifying data, which is idempotent.
    • 401 Unauthorized - [*]: Indicates that the user does not have permission (token, username, password error).
    • 403 Forbidden - [*] indicates that the user is authorized (as opposed to 401 error), but access is prohibited.
    • 404 NOT FOUND - [*]: The user's request is for a non-existent record, the server does not operate, and the operation is idempotent.
    • 406 Not Acceptable - [GET]: The format of user requests is not available (e.g. JSON format is requested by the user, but only XML format).
    • 410 Gone -[GET]: The resource requested by the user is permanently deleted and will not be retrieved.
    • 422 Unprocesable entity - [POST/PUT/PATCH] When an object is created, a validation error occurs.
    • 500 INTERNAL SERVER ERROR - [*]: The server has made an error and the user will not be able to determine whether the request was successful.
  1. SpringBoot Request Method Code Reference

    /**
     * @author wsyjlly
     * @create 2019.06.10 - 19:00
     * GET(SELECT): Extract resources (one or more) from the server.
     * POST(CREATE): Create a new resource on the server.
     * PUT(UPDATE): Update the resource on the server (the client provides the changed full resource).
     * PATCH(UPDATE): Update resources on the server (client provides changed attributes).
     * DELETE(DELETE): Delete resources from the server.
     **/
    @RestController
    @RequestMapping("/cors")
    public class RestfulCorsController {
        @RequestMapping("/")
        public Map<String,String> itemOperator(@RequestBody ModelMap params){
            Map<String,String> map = new HashMap<>();
            map.put("name", (String) params.get("name"));
            System.out.println(params);
            return map;
        }
    
        @PostMapping("/add")
        public Map<String,String> addItem(@RequestBody ModelMap params){
            Map<String,String> map = new HashMap<>();
            map.put("name", (String) params.get("name"));
            System.out.println(params);
            return map;
        }
    
        @DeleteMapping("/{id}")
        public String deleteItem(@PathVariable Long id){
            System.out.println(id);
            return String.valueOf(id);
        }
    
        @PutMapping("/{id}")
        public String updateItem(@PathVariable Long id){
            System.out.println(id);
            return String.valueOf(id);
        }
    
        @PatchMapping("/patch")
        public String updateItem(@RequestBody ModelMap entity){
            System.out.println(entity);
            return "STATUS";
        }
    
        @GetMapping("/{id}")
        @CrossOrigin(value = "*",allowedHeaders = "*",maxAge = 1800)
        public String getItem(@RequestParam(value = "id", required = false, defaultValue = "1601141019") @PathVariable Long id){
            System.out.println(id);
            return String.valueOf(id);
        }
    }

Posted by k_ind on Sun, 11 Aug 2019 23:37:30 -0700