深入淺析Restful接口的兩種使用方式
為什么用restful接口?
怎么用呢?
restful接口常用的兩種方式是get和post.下面簡(jiǎn)單介紹一下這兩種方式的使用.
由于調(diào)用restful接口是通過(guò)url的方式來(lái)訪問(wèn)后端的代碼.新建CustRegisterApi類以后,除了基本的注入外,還需要配置url的地址.以后的demo就在這個(gè)類里面寫了.
<strong><span style="font-size:18px;">@RestController @RequestMapping(value = "/customer/register", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }) @CrossOrigin(origins = "*") public class CustRegisterApi { @Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response; }</span></strong>
1:get方式,url地址會(huì)在地址欄顯示出參數(shù).
<strong><span style="font-size:18px;">/** * 檢查郵箱是否已經(jīng)綁定 * @param email 郵箱 * @return */ @RequestMapping(value = "/checkEmail", method = { RequestMethod.GET }) @ApiOperation(value = "檢查郵箱是否已經(jīng)綁定") public RestResponse<Boolean> checkEmail(@RequestParam(value = "email") String email) { RestResponse<Boolean> restResponse = null; try { boolean checkIsMailBinding = custService.checkIsMailBinding(email); // restResponse = new RestResponse<Boolean>(RestRespCode.OK, MessageUtil.getMessage(RestRespCode.OK), // checkIsMailBinding); if (checkIsMailBinding == false) { restResponse = new RestResponse<Boolean>(RestRespCode.REGISTER_USERNAME_EXISTED, MessageUtil.getMessage(RestRespCode.REGISTER_USERNAME_EXISTED), null); } else { restResponse = new RestResponse<Boolean>(RestRespCode.OK, MessageUtil.getMessage(RestRespCode.OK), null); } } catch (Exception e) { e.printStackTrace(); restResponse = new RestResponse<Boolean>(RestRespCode.INTERNAL_ERROR, MessageUtil.getMessage(RestRespCode.INTERNAL_ERROR), null); } return restResponse; }</span></strong>
訪問(wèn)方式:http://localhost:8080(端口號(hào))/模塊名稱/register/checkEmail?email=****
:post方式,url地址會(huì)在地址欄不會(huì)顯示出參數(shù).
<strong><span style="font-size:18px;">/** * 修改密碼 * @param memberId 用戶編號(hào) * @param oldPassword 舊密碼 * @param newPassword 新密碼 * @return * @throws Exception */ @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST, consumes = "application/json") @ApiOperation(value = "修改支付密碼") public RestResponse<Boolean> changePassword(@RequestBody CaptchaVO captchaVO) throws Exception { // 驗(yàn)證舊密碼是否正確 Boolean findPassword = registerService.findPassword(captchaVO.getMemberId(), captchaVO.getOldPassword()); if (findPassword == false) { return new RestResponse<Boolean>(RestRespCode.PASSWORD_WRONG, MessageUtil.getMessage(RestRespCode.PASSWORD_WRONG), null); } return new RestResponse<Boolean>(RestRespCode.OK, MessageUtil.getMessage(RestRespCode.OK), null); }</span></strong>
post方式
是通過(guò)application/json;charset=utf-8來(lái)訪問(wèn)一級(jí)custom的方式來(lái)訪問(wèn),一般是用于修改密碼或者是不讓別人看到參數(shù)的情況下用的post方式.
在測(cè)試的時(shí)候我是用火狐瀏覽器上的resteasy插件來(lái)進(jìn)行測(cè)試的.
總結(jié)
以上所述是小編給大家介紹的Restful接口的兩種使用方式,希望對(duì)大家有所幫助!
- Yii2.0 RESTful API 基礎(chǔ)配置教程詳解
- SpringBoot2.1 RESTful API項(xiàng)目腳手架(種子)項(xiàng)目
- python用post訪問(wèn)restful服務(wù)接口的方法
- flask-restful使用總結(jié)
- 如何使用Spring RestTemplate訪問(wèn)restful服務(wù)
- 用Node編寫RESTful API接口的示例代碼
- Python利用Django如何寫restful api接口詳解
- Angular(5.2->6.1)升級(jí)小結(jié)
- angular6 填坑之sdk的方法
- Angular使用Restful的增刪改
相關(guān)文章
WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法,涉及C#實(shí)現(xiàn)WinForm窗體全屏顯示的實(shí)現(xiàn)及調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#使用Aspose.Cells創(chuàng)建和讀取Excel文件
這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells創(chuàng)建和讀取Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè)
這篇文章主要介紹了C#中OpenCVSharp實(shí)現(xiàn)輪廓檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11