When rest API makes a modification operation, how to return the modified result

Design the rest interface. For the update,add,delete operation, it is said on the Internet that the modified result should be returned to the caller, and the caller can directly take advantage of the modified result. So whether the return result returns or a primary key id will the entire entity modified by , such as User, return .

question 1:
1 if returns a primary key id .
since the id of update and delete is already passed from the caller, it does not need to be returned. When add, the id is generated in the background, so it needs to be returned to the caller. In this way, even if the unified return object is encapsulated, it feels that the return json of API is still not uniform.

2 if returns the entity .
is not an api for a modification operation to operate the database twice (one modification, one query)

how do you design rest API? Is there any code to share? Thank you!


< table > < thead > < tr > < th > Action < / th > < th align= "center" > request method / URI < / th > < th align= "right" > response status code < / th > < th align= "center" > response data < / th > < / tr > < / thead > < tbody > < tr > < td > create < / td > < td align= "center" > POST / users < / td > < td align= "right" > 201 < / td > < td align= "center" > user entity < / td > < / tr > < tr > < td > Update < / td > < td align= "center" > PUT / users/1 < / td > < td align= "right" > 200 < / td > < td align= "center" > user entity < / td > < / tr > < tr > < td > Delete < / td > < td align= "center" > DELETE / users/1 < / td > < td align= "right" > 204 < / td > < td align= "center" > none < / td > < / tr > < tr > < td > query < / td > < td align= "center" > GET / users < / td > < td align= "right" > 200 < / td > < td align= "center" > user list < / td > < / tr > < tr > < td > query < / td > < td align= "center" > GET / users/1 < / td > < td align= "right" > 200 < / td > < td align= "center" > user entity < / td > < / tr > < / tbody > < / table >
  1. needs to insert the database once when it is created, and returns
  2. If you edit
  3. , query first. If you can't find it, it will be 404. Then insert it and return
  4. .
  5. you can delete it directly. If the number of rows affected is less than 1, 404 will be returned.
  6. there is nothing to say about query

Hello, Restful API is a design style. It's not a mandatory standard
We can compare traditional writing with its traditional writing
traditional writing:
query / user/query?name=tom GET
details / user/getInfo?id=1 GET
create / user/create?name=tom GET
modify / user/update?id=1&name=jerry POST
delete / user/delete?id=1 GET
RESTful
query / user?name=tom GET
details / user/1 GET
create / user POST
modify / user/1 PUT
delete / user/1 DELETE
several remarks about Restful API
1, Use URL to describe resources
2, use HTTP methods to describe behavior, Using HTTP status codes to represent different results
3, using json to interact with data
4, RESTful is just a style, not a mandatory standard
there is also an official model:
Level 0: use Http as the transmission method
Level 1: introduce the concept of resources, each resource has a corresponding URL
Level 2: use HTTP methods for different operations, use HTTP status codes to represent different results
Level 3: use hypermedia, Link information is included in the expression of the resource
similar to developing a query request for Restful API, in SpringMVC:

-- write test cases for RestfulAPI
-- declare RestfulAPI
using annotations-- pass parameters in RestfulAPI

Common annotations
-- @ RestController indicate that this Controller provides RestAPI
-- @ RequestMapping and its variants, mapping http request url to java method
-- @ RequestParam mapping request parameters to java method parameters
-- @ PageableDefault specifies the default value of paging parameters
. For what to return or what to get, it is more often designed according to the focus of the business.


before executing crud, you must perform a query return in the database to determine whether the entity already exists. This is a query. If there is no error message returned directly, if GET returns the entity, if put, first verifies whether the data you passed is in accordance with the format, if the verification passes, update the modified entity with the verified data, and then save the modified entity. Successfully save the database, return the updated entity, and do not need to query again. Therefore, to remove the first verification operation, you only need to query the database


first send the post request to add a record and then return the data primary key ID to the frontend. After receiving the id, the frontend will continue to send a get request to obtain the ID data information and return to the frontend to refresh the display.

Menu