are they implementation dependent?

I'm developing Spring Rest webs service using PUT and POST

@RequestMapping(value = "/test", method = RequestMethod.POST)
@Override
public String function(Model model)
{
}

So, what is the difference between using PUT and POST in this case?

I know that PUT is idempotent, meaning if the same url is called multiple times, the effect should be the same. If I provide the request method as PUT and if I include a DB operation inside the function, won't the meaning of PUT change, meaning if I call the test url multiple times, the DB value will change each time.

My question is does the idempotence, state change, all those features depend on the developer's implementation?

Better example:

@RequestMapping(value = "/test", method=RequestMethod.POST, produces={"application/json"})
public @ResponseBody List<Integer> postData(@RequestParam String name) {        

    if (name.equalsIgnoreCase("okkk")) {
        return returnDataList();
    }else {
        List<Integer> list = new ArrayList<Integer>();
        list.add(12345);
        return list;
    }
}

@RequestMapping(value = "/test/{name}", method=RequestMethod.PUT, produces={"application/json"})
public @ResponseBody List<Integer> putData(@PathVariable String name) {     

    if (name.equalsIgnoreCase("okkk")) {
        return returnDataList();
    }else {
        List<Integer> list = new ArrayList<Integer>();
        list.add(12345);
        return list;
    }

Both the methods are the same, I believe. I just put PUT and POST, a little confused.


Here is the best answer regarding this: What's the difference between a POST and a PUT HTTP REQUEST?

No matter how many times PUT is called it should do exactly the same thing over and over again. PUT responses are not cacheable.

POST allows the web server to decide what to do with the data. These requests can be cached, assuming "the server sets the appropriate Cache-Control and Expires Headers."

There is another resource that I believe can be helpful: PUT vs. POST in REST

The author sums up there very nicely when to use a POST and when to use a PUT. I have selected what should be the most simplistic:

POST: Used to modify and update a resource.

PUT: Used to create a resource, or overwrite it.


Does the idempotence, state change, all those features depend on the developer's implementation?

Yes, it is up to the developer to implement PUT and POST in a way that meets the HTTP specification. If you fail to do that, your service will probably still work but there is a small chance that you will encounter unexpected problems (eg your service might behave weirdly when accessed from behind a proxy).

If I include a DB operation inside the function, won't the meaning of PUT change?

You can perform database operations when you handle a PUT request as long as the effect is idempotent.

For example, if your service lets clients create files using requests such as PUT /myfile with the desired content passed in the body, you could handle each request using a database transaction that does the following:

  • Check if the named file already exists
  • If it already exists, update its content and respond with 200 (OK)
  • Otherwise, create the file and respond with 201 (Created)
  • This is idempotent, because if the same request is made a second time the state of the database will not change.

    For a non-idempotent example, suppose step 2 was changed to the following:

  • If it already exists, append the new content to the existing content
  • This is not idempotent, because if a client sends the same request twice the file will have two copies of the content.

    链接地址: http://www.djcxy.com/p/41050.html

    上一篇: 什么时候应该使用GET或POST方法? 他们之间有什么区别?

    下一篇: 它们是否依赖于实现?