Build RESTful Style Application in Spring Boot

Posted by kontesto on Sat, 07 Sep 2019 04:28:53 +0200

RESTful architecture is one of the most popular Internet software architectures.It is clear, standard-compliant, easy to understand and easy to expand, so it is being used by more and more websites.

1. What is a RESTful architecture

1.1 REST Term Interpretation

REST -- REpresentational State Transfer
First, the obscurity is due to the removal of the previous subject, which is called Resource Representational State Transfer:

  • Resource: Resource, that is, data.Each resource corresponds to a specific URI.
  • Representational: The representation of a resource, such as JSON, XML, JPEG, etc.
  • State Transfer: State change.This kind of transformation is based on the representation layer, so it is "the representation layer state transformation".Through HTTP verbs (GET, POST, PUT, DELETE).

Taken together, let's summarize what RESTful architecture is:

  1. Each URI represents a resource;
  2. A layer of presentation that passes this resource between the client and the server;
  3. Clients operate on server-side resources through four HTTP verbs to achieve "representation layer state transition".

1.2 RESTful Design Misconceptions

  1. Include verbs in URI

Since Resource represents an entity, it should be a noun, URI should not have verbs, and verbs should be placed in the HTTP protocol.For example:

 POST api/user/get/{id}  //Wrong URI, where get is a verb, incorrectly designed
 GET api/user/{id}  //Correct URI

 POST api/deleteUser/{id}  //Wrong URI where delete is a verb is incorrectly designed
 DELETE api/user/{id}  //Correct URI

If some actions cannot be represented by HTTP verbs, you should make them a resource.For example, online remittance, remit 500 yuan from account 1 to account 2:

 POST /accounts/1/transfer/500/to/2  //Error URI

 /**The correct way to write it is to change the verb transfer to the noun transaction. A resource cannot be a verb, but it can be a service: **/
 POST /transaction HTTP/1.1
  Host: 127.0.0.1
  from=1&to=2&amount=500.00
  1. Add version number to URI

Because different versions can be interpreted as different representations of the same resource, the same URI should be used.Version numbers can be distinguished in the Accept field of the HTTP request header information.For example:

 http://www.example.com/app/1.0/foo//Error URI
 http://www.example.com/app/1.1/foo//Error URI

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

2. Why use RESTful architecture

RESTful architecture is one of the most popular Internet software architectures.It is clear, standard-compliant, easy to understand and easy to expand, so it is being used by more and more websites.

  1. The RESTful architecture is entirely based on the HTTP protocol, making full use of the features of the HTTP protocol, and distinguishes different operations such as acquisition, addition, update and deletion by using HTTP verbs such as GET, POST, PUT, DELETE through a unified interface.
  2. Resource-oriented, self-explanatory at a glance.In the RESTful architecture, everything is a resource, and a user accessing a Web site through a URL requests a resource on the Web site server.Each URI identifies a unique resource.
  3. Data description is simple, usually using XML, JSON for data exchange (JSON is used mainly now).

3. How to use the RESTful architecture

Use RESTful to implement add-delete check interfaces:

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

    // Add operation POST api/user/
    @RequestMapping(value = "/" , method = RequestMethod.POST)
    public Result add(@RequestBody User user){
        //...specific interface implementation
    }

    // Delete operation DELETE api/user/{id}
    @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        //...specific interface implementation
    }

    // Update operation PUT api/user/{id}
    @RequestMapping(value = "/{id}" , method = RequestMethod.PUT)
    public Result update(@PathVariable String id , @RequestBody User user){
        //...specific interface implementation
    }

    // Query operation GET api/user/{id}
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    public Result query(@PathVariable String id){
        //...specific interface implementation
    }

    // Query list operation GET api/user/list
    @RequestMapping(value = "/list" , method = RequestMethod.GET)
    public Result list(){
        //...specific interface implementation
    }
}

Have you learned how to build RESTful style applications in Spring Boot?

Reference documents: Understanding RESTful Architecture--Ruan Yifeng's Web Log

Topics: Java JSON REST xml Spring