How to choose put and post in SpringMVC

  1. Some people believe that POST should be used to create a resource, and PUT should be used to update a resource;
  2. Some people think that PUT should be used to create a resource, and POST should be used to update a resource;
  3. There are also views that either PUT and POST can be used to create or update a resource.

These points of view only see the style, and they just argue about which style is better. In fact, whether to use PUT or POST is not a matter of creating or updating resources. This is not a question of style, but a question of semantics.

To give a simple example, if there is a blog system that provides a Web API, the model is http://superblogging/blogs/{blog-name}, it is very simple, replace {blog-name} with our blog name, go to This URI sends an HTTP PUT or POST request. The body part of HTTP is the blog post. This is a very simple REST API example. Should we use the PUT method or the POST method? Depends on whether the behavior of this REST service is idempotent, if we send two http://superblogging/blogs/post/Sample requests, what kind of behavior is on the server side? If two blog posts are generated, it means that this service is not idempotent, because multiple use has side effects; if the first request is overwritten by the latter request, then the service is idempotent.

In the former case, the POST method should be used, and in the latter case, the PUT method should be used.

Current project: insert --> post; update --> put

Guess you like

Origin blog.csdn.net/guorui_java/article/details/109715075