About the RESTful API

Related information:

RESTFUL is a web application design style and development mode based on HTTP. It can be defined in XML format or JSON format. RESTFUL Applies to scenarios where a mobile Internet vendor functions as a service enabling interface to invoke mobile network resources by a third-party OTT. The actions can be added, changed, or deleted.

It is important to note that REST does not have a clear standard, but rather a style of design.

Important: RESTful is a URL design style.

Interpretation: There are no strict syntactic conventions for RESTful usage. There is no “what conditions must be met to be RESTful”, nor “what conditions must not be RESTful”. Typically, with RESTful apis, the response data to the client is in XML or JSON format, known as the “response body,” and is developed in a way that separates the front and back ends.

In RESTful architecture, browsers use POST, DELETE, PUT, and GET requests to add, DELETE, modify, and query specified URL resources. Therefore, RESTful uses URI to manage and access resources. RESTful features strong scalability and clear structure.

For example, the core of “register” is to insert user data, the request mode of POST should be used, while the core of “change password” is to update data, the operation of PUT should be used, and so on. However, based on the usage habits of developers, even some complex services may contain a variety of data operations in addition, deletion, modification, and query. It is impossible to define exactly which operations are involved. Therefore, only POST and GET request methods are used in common practice, and PUT and DELETE are not used.

For example, the following URL can be considered RESTful:

https://blog.csdn.net/x541211190/article/details/81141459
https://blog.csdn.net/wl_1013/article/details/81049691
Copy the code

In fact, the RESTful style has a very typical characteristic: core parameters are passed directly as part of the URL, rather than as parameters!

If not RESTful, the above URL might need to be designed as:

https://blog.csdn.net/article/details/?username=x541211190&id=81141459
https://blog.csdn.net/article/details/?username=wl_1013&id=81049691
Copy the code

Using RESTful style, you can make urls more concise and easier to read or understand!

The above example is only that CSDN is designed in this way. The first level after the domain name in the URL is fixed as “user name”, and the first level after details is fixed as THE ID value. It does not mean that other sites must be designed in this way, or even almost all other sites are not designed in this way. RESTful itself is not a convention!

If there is no specific agreement, the following style can be adopted:

/resources/id/command
Copy the code

or

/resouces/id/property/command
Copy the code

Among the above design styles:

  • Resources: What kind of data needs to be accessed;
  • Id: unique identifier of data. If only one data item needs to be accessed and its ID needs to be public, add this parameter.
  • Property: Which property of a piece of data to access;
  • Command: Which operation needs to be performed on a piece of data or an attribute of a piece of data.

For example, it could be designed as:

/users/password/change
/addresses/10/set_default
Copy the code

Note: If a parameter value is placed in the URL, it must be “unique”; otherwise, it should not be placed in the URL.

The SpringMVC framework supports RESTful style! When designing the request path, if the request path contains a variable parameter value, use {} to enclose the custom name, for example:

@RequestMapping("{aid}/set_default")
Copy the code

In the argument list of the method that handles the request, the @pathvariable annotation gets the value of the placeholder in the URL:

@RequestMapping("{aid}/set_default")
public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid) {}Copy the code