I recently learned the RESTful interface specification and used Postman to introduce it. Here are the relevant RESTful interface specifications: 1. What is RESTful?
REST: It is an architectural style for web services; it uses widely popular standards and protocols such as HTTP, URI, XML, JSON, and HTML; it is lightweight, cross-platform, and cross-language architectural design; it is a design style, not A standard is an idea.
RESTful: The corresponding Chinese is REST; Restful web service is a common REST application, which is a web service that complies with REST style; REST web service is a kind of ROA (The Resource-Oriented Architecture) (resource-oriented architecture) Architecture) [Recommendation: web front end]
The essence of RESTful is a software architecture style. The core is resource-oriented and solves the following problems:
Reduce development complexity
Improve system scalability
2.The difference between SOAP and REST:
SOAP (Simple Object Access Protocol): A protocol specification for exchanging data. It is a lightweight, simple, XML-based protocol.
REST (Representational State Transfer): A software architecture style that can reduce the complexity of development and improve the scalability of the system.
Efficiency and ease of use :
SOAP : Due to the continuous expansion of the content of its own protocol due to various needs, the performance of SOAP processing has declined. At the same time, the ease of use and learning costs have also increased.
RESTful : Due to its resource-oriented interface design and operation abstraction, it simplifies developers' bad designs, and also makes maximum use of the original application protocol design concept of HTTP.
Security :
RESTful is very suitable for resource-based service interfaces, and is especially suitable for scenarios that require high efficiency but low security requirements.
The maturity of SOAP can bring convenience to the design of interfaces that need to be provided for multiple development languages and have higher security requirements.
In summary:
Security : SOAP will be better than REST;
Efficiency and ease of use : REST is better;
Maturity : Generally speaking, SOAP is superior to REST in terms of maturity.
3. How to design RESTful API :
Resource path : In the RESTful architecture, each URL represents a resource, so the URL cannot have verbs, only nouns. Generally speaking, nouns in APIs should be plural.
HTTP verb : Operation of resources (CURD), represented by HTTP verb (predicate).
GET: Retrieve resources (one or more items) from the server.
POST: Create a new resource on the server.
PUT: Update resources on the server (the client provides the complete changed resources).
DELETE: Delete the resource from the server.
Let’s take the zoo as an example (including information about various animals and employees):
https://api.example.com/v1/zoos //Zoo resources https://api.example.com/v1/animals //Animal resources https://api.example.com/v1/employees //Employees resource
POST /zoos: Create a new zoo
GET /zoos/ID: Get information about a specified zoo
PUT /zoos/ID: Update the information of a specified zoo
DELETE /zoos/D: Delete a zoo
Filter information :
?limit=10: Specify the number of records returned
?offset=10: Specifies the starting position of the returned record.
?page=2&per_page=100: Specify which page and number of records per page.
?sortby=name&order=asc: Specify which attribute the returned results are sorted by, and the sorting order.
?animal_type_id=1: Specify filter conditions
Status code :
200 (OK) - if an existing resource has been changed
201 (created) - if a new resource is created
202 (accepted) - The processing request has been accepted but has not been completed (asynchronous processing)
301 (Moved Permanently) - The URI of the resource was updated
303 (See Other)-Others (such as load balancing)
400 (bad request) - refers to a bad request
404 (not found)-The resource does not exist
406 (not acceptable) - The server does not support the required representation
409 (conflict) - Generic conflict
412 (Precondition Failed)-The precondition failed (such as a conflict when executing condition updates)
415 (unsupported media type) - The received representation is not supported
500 (internal server error)-generic error response
503 (Service Unavailable) - The service is currently unable to handle the request
Error handling :
The returned information uses error as the key name and error information as the key value.
{ error: “Invalid API key” }
Return results :
GET /collection: Returns a list (array) of resource objects
GET /collection/resource: Returns a single resource object
POST /collection: Returns the newly generated resource object
PUT /collection/resource: Returns the complete resource object
PATCH /collection/resource: Returns the complete resource object
DELETE /collection/resource: returns an empty document
4.REST style interface testing process :
Let’s introduce it directly with a picture.
5. Code writing and Postman testing :
Tools: idea, mysql database, Postman
Database table data:
Entity class:
Front-end (vue) entry to mastery course: enter learning
@Data @ApiModel("User Entity Class") public class User extends Model<User> implements Serializable { @ApiModelProperty("userid") @NotNull(message = "User ID cannot be empty") private Integer id; @NotBlank(message = "Username cannot be blank") @ApiModelProperty("username") private String name; @Min(1) @Max(100) @ApiModelProperty("User age") private Integer age; @NotEmpty(message = "The mailbox cannot be empty") @ApiModelProperty("User Email") @Email private String email; @ApiModelProperty("0: Not deleted 1: Deleted") @TableLogic private Integer deleted; @NotBlank(message = "Password cannot be blank") @ApiModelProperty("User password") private String password; }
Controller layer code (service layer omitted):
@Api(tags = "User Management") @RestController @RequestMapping("/MyWebsite/user") public class UserController { @Autowired private UserServiceImpl userServiceimpl; @ApiOperation(value = "New User") @PostMapping public int insertUser(User user) { System.out.println("Add successfully"); return userServiceimpl.insertUser(user); } @ApiOperation(value = "Modify user information") @PutMapping public void updateUser(@RequestBody @Valid User user) { userServiceimpl.updateUser(user); System.out.println("Modification successful"); } @ApiOperation(value = "Delete user", notes = "Delete user based on id") @DeleteMapping("/{id}") public int deleteUser(@ApiParam("user id") @PathVariable @Valid Integer id) { System.out.println("Deletion successful"); return userServiceimpl.deleteUser(id); } @ApiOperation(value = "Query all users") @GetMapping public List<User> allUser() { System.out.println("Query successful"); return userServiceimpl.allUser(); } @ApiOperation(value = "id query user") @GetMapping("/{id}") public User selectById(@PathVariable("id") String id) { return userServiceimpl.selectById(id); } }
Use Postman to test (url path must correspond):
Query all users : http://localhost:8081/MyWebsite/user
Header: Content-Type=application/json
Body: empty
Query successful!
Query based on id:
Query successful!
Add new user :
Check that the database has been added successfully!
Modify user information (modify name and age with ID 7):
Check that the database modification was successful!
Delete users based on id :
Check that the database was deleted successfully! (deleted=1)
Maybe you deleted the entire piece of data directly
When querying or updating or deleting non-existent data (or the data is illegal), the execution fails. There are other interfaces, such as deleting all interfaces. I have not tested them one by one here. If there are any errors, please point them out.
The above is the detailed introduction of restful interface specification and postman debugging tool!