Restful 風(fēng)格的 API 是一種軟件架構(gòu)風(fēng)格即纲,設(shè)計(jì)風(fēng)格而不是標(biāo)準(zhǔn),只是提供了一組設(shè)計(jì)原則和約束條件博肋。它主要用于客戶端和服務(wù)器交互類的軟件低斋》涮基于這個風(fēng)格設(shè)計(jì)的軟件可以更簡潔,更有層次拔稳,更易于實(shí)現(xiàn)緩存等機(jī)制葛峻。
在 Restful 風(fēng)格中,用戶請求的 url 使用同一個 url 而用請求方式:get巴比,post术奖,delete,put...等方式對請求的處理方法進(jìn)行區(qū)分轻绞,這樣可以在前后臺分離式的開發(fā)中使得前端開發(fā)人員不會對請求的資源地址產(chǎn)生混淆和大量的檢查方法名的麻煩采记,形成一個統(tǒng)一的接口。
SpringMVC Restful 風(fēng)格 url 配置實(shí)現(xiàn)的方式
SpringMVC 的 resturl 是通過 @RequestMapping 及 @PathVariable annotation 提供的政勃,通過如 @RequestMapping(value="/blog /{id}",method=RequestMethod.DELETE) 即可處理 /blog/1 的 delete 請求唧龄。
GET(SELECT):從服務(wù)器查詢,可以在服務(wù)器通過請求的參數(shù)區(qū)分查詢的 方式奸远。
POST(CREATE):在服務(wù)器端新建一個資源既棺,調(diào)用 insert 操作。
PUT(UPDATE):在服務(wù)器端更新資源懒叛,調(diào)用 update 操作丸冕。
PATCH(UPDATE):在服務(wù)器端更新資源(客戶端提供改變的屬性)。(目前 jdk7 未實(shí)現(xiàn)薛窥,tomcat7 不支持)胖烛。
DELETE(DELETE):從服務(wù)器端刪除資源,調(diào)用 delete 語句诅迷。
案例實(shí)操
Get 請求配置
/**
*restful-->get 請求 執(zhí)行查詢操作
* @param id
* @return
*/
@RequestMapping(value="queryAccountById02/{id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
publicMessageModelqueryAccountById(@PathVariableIntegerid){
MessageModelmessageModel=newMessageModel();
if(null==id){
messageModel.setCode(300);
messageModel.setMsg("參數(shù)非法!");
returnmessageModel;
?? }
messageModel.setResult(accountService.queryById(id));
returnmessageModel;
}
Post 請求配置
/**
* restful-->post 請求執(zhí)行添加操作
* @param id
* @param aname
* @return
*/
@RequestMapping(value="saveAccount",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
publicMessageModelqueryAccountById04(@RequestBodyAccountaccount){
MessageModelmessageModel=newMessageModel();
try{
? ? accountService.saveOrUpdateAccount(account);
}catch(ParamsExceptione) {e.printStackTrace();
messageModel.setCode(e.getErrorCode());
messageModel.setMsg(e.getErrorMsg());
}catch(Exceptione) {e.printStackTrace();
messageModel.setCode(300);
messageModel.setMsg("操作失敗!");
?? }
returnmessageModel;
}
Put 請求配置
/**
* restful-->put 請求執(zhí)行更新操作
* @param id
* @param account
* @return
*/
@RequestMapping(value="update/{id}",method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
publicMessageModelqueryAccountById04(@PathVariableIntegerid,@RequestBodyAccountaccount){
MessageModelmessageModel=newMessageModel();
try{
? ? accountService.saveOrUpdateAccount(account);
}catch(ParamsExceptione) {e.printStackTrace();
? ? messageModel.setCode(e.getErrorCode());
? ? messageModel.setMsg(e.getErrorMsg());
}catch(Exceptione) {e.printStackTrace();
? ? messageModel.setCode(300);
? ? messageModel.setMsg("操作失敗!");
?? }
returnmessageModel;
}
Delete 請求配置
/**
* restful-->delete 請求 執(zhí)行刪除操作
* @param id
* @return
*/
@RequestMapping(value="deleteAccountById/{id}",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
publicMessageModelqueryAccountById05(@PathVariableIntegerid){
MessageModelmessageModel=newMessageModel();
try{
? ? accountService.deleteAccountById(id);
}catch(ParamsExceptione) {e.printStackTrace();
messageModel.setCode(e.getErrorCode());
messageModel.setMsg(e.getErrorMsg());
}catch(Exceptione) {e.printStackTrace();
messageModel.setCode(300);
messageModel.setMsg("操作失敗!");
?? }
returnmessageModel;
}
擴(kuò)展~REST
REST(英文:Representational State Transfer佩番,簡稱REST)描述了一個架構(gòu)樣式的網(wǎng)絡(luò)系統(tǒng),比如 web 應(yīng)用程序罢杉。它首次出現(xiàn)在 2000 年 Roy Fielding 的博士論文中趟畏,Roy Fielding是 HTTP 規(guī)范的主要編寫者之一。在目前主流的三種Web服務(wù)交互方案中滩租,REST相比于SOAP(Simple Object Access protocol赋秀,簡單對象訪問協(xié)議)以及XML-RPC更加簡單明了,無論是對URL的處理還是對Payload的編碼持际,REST都傾向于用更加簡單輕量的方法設(shè)計(jì)和實(shí)現(xiàn)沃琅。值得注意的是REST并沒有一個明確的標(biāo)準(zhǔn),而更像是一種設(shè)計(jì)的風(fēng)格蜘欲。
REST 指的是一組架構(gòu)約束條件和原則益眉。滿足這些約束條件和原則的應(yīng)用程序或設(shè)計(jì)就是 RESTful。