Search code examples
javapostput

PUT and POST - 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.


Solution

  • 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.