Springcloud中Feign傳遞參數(shù)的過(guò)程解析
傳遞單個(gè)參數(shù):
單個(gè)參數(shù)的傳值有兩種方式,第一種使用@RequestParam/@PathVariable進(jìn)行傳值
客戶端feign調(diào)用接口(@RequestParam)
@RequestMapping("/ct/selectOne") Customer selectOne(@RequestParam("id") Integer id);
服務(wù)提供端
@RequestMapping("selectOne") public Customer selectOne(Integer id) { return this.customerService.queryById(id); }
客戶端feign調(diào)用接口(@PathVariable)
@GetMapping("/admin/selectOne/{id}") String selectOne(@PathVariable("id") Integer id);
服務(wù)提供端
@RequestMapping("selectOne/{id}") @HystrixCommand(fallbackMethod = "HystrixqueryById") public Admin selectOne(@PathVariable("id") Integer id) { Admin bean = adminService.queryById(id); if(bean == null){ throw new RuntimeException("id:"+id+"沒(méi)有找到該id的用戶"); } return bean; }
注意:
1、在使用@RequestParam/@PathVariable進(jìn)行傳值時(shí),一定要注意,需要綁定參數(shù),如@RequestParam(“id”)綁定id,不然會(huì)報(bào)錯(cuò)
2、@PathVariable是獲取url上數(shù)據(jù)的,@RequestParam獲取請(qǐng)求參數(shù)的(包括post表單提交)
傳遞多個(gè)參數(shù):多個(gè)參數(shù)的傳值可以使用多個(gè)@RequestParam來(lái)進(jìn)行傳參
客戶端feign調(diào)用接口
@RequestMapping("/ct/upload") Customer upload(@RequestParam("newFileName") String newFileName, @RequestParam("id") int id);
服務(wù)提供端
@RequestMapping("upload") public Customer upload(String newFileName,int id) throws IOException { System.out.println("進(jìn)入提供者-圖片上傳"); //設(shè)置圖片上傳路徑,是目標(biāo)文件夾的路徑 // 保存到數(shù)據(jù)庫(kù) Customer customer=customerService.queryById(id); customer.setImage(newFileName); customerService.update(customer); return customer; }
傳對(duì)象:
傳對(duì)象有兩種方式
第一種,使用@SpringQueryMap注解實(shí)現(xiàn)
客戶端feign調(diào)用接口
@RequestMapping("/ev/insert") Evaluation insert(@SpringQueryMap Evaluation evaluation);
服務(wù)提供端
@PostMapping("save") public Object save(@RequestBody Admin admin){ boolean result = false; //判斷是添加還是編輯 if(admin.getId()!=null){ //編輯 // System.out.println("編輯管理員信息"); result = adminService.update(admin)>0; } else { //添加 admin.setRegDate(new Date()); // System.out.println("添加管理員信息"+admin); result = adminService.insert(admin).getId() != null; } return result; }
重點(diǎn):多個(gè)參數(shù)+對(duì)象的傳值
在進(jìn)行多個(gè)參數(shù)+對(duì)象傳值時(shí),使用@RequestParam來(lái)傳遞普通參數(shù),使用@SpringQueryMap來(lái)傳遞對(duì)象
注:本人親測(cè)踩坑使用@RequestParam+@RequestBody的時(shí)候,出現(xiàn)問(wèn)題@RequestBody要求前端頁(yè)面返回json格式,否則會(huì)報(bào):不支持Content-Type:application/json的錯(cuò)誤
客戶端feign調(diào)用接口
@RequestMapping(value = "/admin/queryAll", method = RequestMethod.POST) String queryAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @SpringQueryMap AdminQuery admin);
服務(wù)提供端
@PostMapping("queryAll") public Object queryAll(Integer page, Integer limit,AdminQuery admin) { CommonResult<Admin> result = new CommonResult<>(); IPage<Admin> ipage = adminService.queryAllByLimit(page,limit,admin); result.setCode(0); result.setCount(ipage.getTotal()); result.setData(ipage.getRecords()); return result; }
到此這篇關(guān)于Springcloud中Feign傳遞參數(shù)的文章就介紹到這了,更多相關(guān)Springcloud中Feign傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-data-redis自定義實(shí)現(xiàn)看門(mén)狗機(jī)制
redission看門(mén)狗機(jī)制是解決分布式鎖的續(xù)約問(wèn)題,本文主要介紹了spring-data-redis自定義實(shí)現(xiàn)看門(mén)狗機(jī)制,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03springboot網(wǎng)站應(yīng)用使用第三方qq登錄的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了springboot網(wǎng)站應(yīng)用使用第三方qq登錄,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Java基于Socket實(shí)現(xiàn)HTTP下載客戶端
這篇文章主要介紹了Java基于Socket實(shí)現(xiàn)HTTP下載客戶端的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-01-01使用java采集京東商城區(qū)劃數(shù)據(jù)示例
這篇文章主要介紹了java采集京東的全國(guó)區(qū)劃數(shù)據(jù)示例,保存成json形式,如想轉(zhuǎn)換到數(shù)據(jù)庫(kù)只需反序列化為對(duì)象保存到數(shù)據(jù)庫(kù)即可2014-03-03mybatis-plus 攔截器敏感字段加解密的實(shí)現(xiàn)
數(shù)據(jù)庫(kù)在保存數(shù)據(jù)時(shí),對(duì)于某些敏感數(shù)據(jù)需要脫敏或者加密處理,本文主要介紹了mybatis-plus 攔截器敏感字段加解密的實(shí)現(xiàn),感興趣的可以了解一下2021-11-11Java使用CompletableFuture實(shí)現(xiàn)異步編程
在現(xiàn)代 Java 開(kāi)發(fā)中,異步編程是一項(xiàng)重要技能,而 CompletableFuture 是從 Java 8 開(kāi)始提供的一個(gè)功能強(qiáng)大的工具,用于簡(jiǎn)化異步任務(wù)的編寫(xiě)和組合,本文將詳細(xì)介紹 CompletableFuture 的基本使用和一些常見(jiàn)的應(yīng)用場(chǎng)景,需要的朋友可以參考下2025-01-01