Style building RESTful applications in Spring Boot

Style building RESTful applications in Spring Boot

RESTful architecture, which is currently the most popular Internet software architecture. It is clear the structure, standards-compliant, easy to understand, easy to expand, it is being adopted by more and more websites.

1. What is RESTful architecture

1.1 REST Glossary

REST - Representational State Transfer
First of all, the reason is obscure because before the subject was removed, stands for Resource Representational State Transfer:

  • Resource: resource, namely data. Each resource corresponds to a specific URI.
  • Representational: manifestation of the resource, such as using JSON, XML, JPEG and so on.
  • State Transfer: change status. And this transformation is based on the presentation layer, so is the "presentation layer state transformation." HTTP verb achieve (GET, POST, PUT, DELETE) by.

Integrated the above explanation, we summarize what is RESTful architecture:

  1. Each URI represents a resource;
  2. Between the client and the server, passing some kind of presentation layer of such resources;
  3. Four client via HTTP verbs, operation of server resources, to achieve "the state of the presentation layer transformation."

1.2 RESTful design errors

  1. In the URI contains a verb
    as "resource" means an entity so it should be a noun, URI should not have a verb, the verb should be placed in the HTTP protocol. E.g:
 POST api/user/get/{id}  //错误 URI,其中get是动词,这个URI就设计错了
 GET api/user/{id}  //正确 URI
 POST api/deleteUser/{id}  //错误 URI,其中delete是动词,这个URI就设计错了
 DELETE api/user/{id}  //正确 URI

If some action is not represented HTTP verb, the action you should make a resource. Such as online remittance, remittance 500 yuan 1 from the account to the account 2:

 POST /accounts/1/transfer/500/to/2  //错误 URI
 /**正确的写法是把动词transfer改成名词transaction,资源不能是动词,但是可以是 一种服务:**/
 POST /transaction HTTP/1.1
  Host: 127.0.0.1
  from=1&to=2&amount=500.00
  1. Join the version number in the URI
    because different versions, can be understood as different manifestations of the same resources, so you should use the same URI. The version number can be distinguished in the Accept header field of the HTTP request. E.g:
 http://www.example.com/app/1.0/foo  //错误 URI
 http://www.example.com/app/1.1/foo  //错误 URI

 Accept: vnd.example-com.foo+json; version=1.0  //正确
 Accept: vnd.example-com.foo+json; version=1.1  //正确

2. Why use a RESTful architecture

RESTful architecture, which is currently the most popular Internet software architecture. It is clear the structure, standards-compliant, easy to understand, easy to expand, it is being adopted by more and more websites.

  1. RESTful HTTP protocol architecture based entirely on the full use of the characteristics of the HTTP protocol through a unified interface, using the GET, POST, PUT, DELETE and other HTTP verbs to distinguish get, add, update and delete different operations.
  2. Resource-oriented, clear, self-explanatory. Everything is in RESTful architecture resources, users access the site through the URL is requested resources on the web server. Every URI designates a unique resource.
  3. Data description simple, generally XML, JSON for data exchange (now mainly use JSON).

3. How to use RESTful architecture

Use RESTful implement CRUD interfaces:

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

    // 增加操作  POST api/user/
    @RequestMapping(value = "/" , method = RequestMethod.POST)
    public Result add(@RequestBody User user){
        //...具体接口实现
    }

    // 删除操作  DELETE api/user/{id}
    @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        //...具体接口实现
    }

    // 更新操作  PUT api/user/{id}
    @RequestMapping(value = "/{id}" , method = RequestMethod.PUT)
    public Result update(@PathVariable String id , @RequestBody User user){
        //...具体接口实现
    }

    // 查询操作  GET api/user/{id}
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    public Result query(@PathVariable String id){
        //...具体接口实现
    }

    // 查询列表操作  GET api/user/list
    @RequestMapping(value = "/list" , method = RequestMethod.GET)
    public Result list(){
        //...具体接口实现
    }
}

Use Postman call:



4. concerned about my micro-channel public number to see more articles, the first time I received the article.

Style building RESTful applications in Spring Boot, you learn it?

Reference document: understand RESTful architecture - Ruan Yifeng's blog

Guess you like

Origin www.cnblogs.com/lixiaofeng101/p/11482396.html