我是新手,对 get、post、put、delete、option、head 等 http 方法感到困惑,任何人都可以分享我一个简单的例子。
这是我的例子:
@GET
@Produces("text/plain")
public String getIt() {
return "Hi there!";
}
@DELETE
@Produces("text/plain")
@Path("/getItDelTest")
public String getItDelTest()
{
return "Hi there is getITDelTest method";
}
@HEAD
@Produces("text/plain")
@Path("/getItHeadTest")
public String getItHeadTest()
{
return "Hi there is getITHeadTest method";
}
@PUT
@Produces("text/plain")
@Path("/getItPutTest")
public String getItPutTest()
{
return "Hi there is getITPutTest method";
}
@POST
@Produces("text/plain")
@Path("/getItPost")
public String getItPost()
{
return "Hi there is getItPost method";
}
Run Code Online (Sandbox Code Playgroud)
在这个例子上面,我只是创建了一个具有 diffrent-2 性质的简单方法,但我不明白为什么我们需要所有这些,如果我们能够通过 post 完成所有这些操作
GET方法仅用于数据检索,不应有任何副作用。但POST旨在实现特定目的:更改服务器端的数据。
要更改资源的状态或更新资源,请使用 PUT;要移除或删除资源,请使用 DELETE。
请阅读:https ://www.ibm.com/developerworks/webservices/library/ws-restful/