With the rise of the mobile Internet, RESTful API design has become popular, but what exactly is RESTful design? This article takes you to see what it really looks like.

RESTful concept

First, we need to make it clear that RESTful is an idea, a design specification, not a protocol, etc.

REST, short for Representational State Transfer, represents layer State transformation. The translation is obscure because it lacks a subject, which should be “Resource Representational State Transfer.”

Generally speaking, it means that “resources are transformed into states in a certain form in the network”.

The concept of REST was developed by Roy Thomas Fielding in his doctoral thesis in 2000, who was the principal designer of the HTTP protocol (versions 1.0 and 1.1).

Now that we know the basic concepts of RESTful, let’s take a look at the explanations of the related concepts.

resources

“Resource” is one of the most central concepts in RESTful. In RESTful concepts, every piece of information on the Internet can be defined as a resource, such as text, images, audio, video, and so on. These resources, in turn, can correspond to a specific URI(Uniform resource Locator), which is the address or unique identifier of each resource.

The presentation layer

For the above “resources”, we need to make corresponding presentation, and can adopt a variety of presentation forms, and these forms of presentation are called “presentation layer”.

For example, text can be rendered in JSON format, XML format, HTML format, even binary format. That’s what the presentation layer does.

State transition

Resources are usually stored on the server, and the operation of adding, deleting, modifying, and searching server resources on the client involves the transformation of resource status. This process is called “state transition”.

Taking HTTP as an example (RESTful is not only applicable to HTTP, but is often used as a foil), the client can change the resources on the server through some operations.

This whole process is called “presentation state transition”.

HTTP provides four common operations: GET, POST, PUT, and DELETE.

These operations correspond to four basic operations: GET to obtain resources, POST to create resources (or update resources), PUT to update resources, and DELETE to DELETE resources.

Why use RESTful style

This is because RESTful design is clear, standards-compliant, easy to understand, and easy to extend.

Imagine, if the traditional JSP mode is adopted, the page content and code are mixed together, and at this time, the project functions need to add mobile terminal, wechat small program and other clients, whether to redefine the interface?

The RESTful definition is not only clear in structure, but more importantly, easy to expand and more widely applicable.

RESTful Style Example

Take the User’s add, delete, change, query as an example, we can design the following interface form: each column corresponds to, (request type: request address: Function description)

  • Get: /user/list: obtains information about all users
  • Post: /user: creates user information
  • Put: /user: updates user information
  • Get: /user/1: obtains information about the user whose resource ID is 1
  • Delete: /user/1: deletes the information about the user whose resource ID is 1

Does it look simple, clear, and much more convenient?

If combined with SpringBoot, it is even more convenient. Some of the code for implementing the Controller layer through SpringBoot is shown below.

@RestController @RequestMapping("/user") public class RestfulController { @Resource private UserService userService; @postmapping public User addUser(User User) {return userservice.adduser (User); } @getMapping ("/list") public list <User> listUser() {return userservice.findAll (); } @putMapping public User update(User User) {return userservice.update (User); } / modify User name * * * * / @ PatchMapping public User updateUsername (User User) {return userService. UpdateUsername (User); } @getMapping ("{id}") public User get(@pathVariable ("id") Long id) {return userService.findById(id); } /** * delete user */ @deletemapping ("{id}") public void delete(@pathVariable ("id") Long id) {userservice.delete (id); }}Copy the code

Boutique SpringBoot 2.x video tutorials

Spring Boot 2.x video tutorial, Spring Boot 2.x video tutorial, Spring Boot 2.x video tutorial, Spring Boot 2.


Program new horizon