Writing REST APIs in Spring Boot

First, use Spring Boot to write RestfulAPI ideas

       1. Describe resources with URLs

       2. Use HTTP methods to describe behavior. Use HTTP status codes to indicate different results

       3. Use json to interact data

       4.Restful is just a style, not a mandatory standard

2. REST Maturity Model

       Level0 uses HTTP as the transport.

       Level1 refers to the resource concept. Each resource has a corresponding URL.

       Level2 uses HTTP methods for different operations and uses HTTP status codes to indicate different operations.

       Level 3 uses hypermedia to include link information in the representation of resources.

3. Write a Restful API

         Write test cases against Restful API

         Reference test in Pom

 
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>


       Fourth, use annotations to declare common annotations of Restful API

          @RestController indicates that this Controller provides RestAPI

          @RequestMapping and its variant (GetMapping). Map http request url to java method

          @RequestParam maps request parameters to java method parameters

          @PageableDefault specifies that the default value of the paging parameter is the Pageable service

          @PathVariable passes a single parameter

5. Passing parameters in Restful API

      Spring Data provides Pageable parameters to pass paging parameters

      Note: For details of jsonpath, please refer to: https://github.com/json-path/JsonPath

6. Use regular expressions in urls    

    @GetMapping("/{id:\\d+}")

    @JsonView controls the json output content

    You can set the json content returned by the method and return it as required

    @RequestBody maps the request body to the parameters of the java method

Reference: https://github.com/fkandy/imooc-security/tree/master/imooc-security-demo/src/main/java/vip/fkandy


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443372&siteId=291194637