SpringCloud Feign參數(shù)問題及解決方法
這篇文章主要介紹了SpringCloud Feign參數(shù)問題及解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
今天遇到使用Feign調(diào)用微服務(wù),傳遞參數(shù)時遇到幾個問題
1.無參數(shù)
以GET方式請求
服務(wù)提供者
@RequestMapping("/hello") public String Hello(){ return "hello,provider"; }
服務(wù)消費者
@GetMapping("/hello") String hello();
2.單個參數(shù)
(1)GET——@PathVariable
服務(wù)提供者
@GetMapping("/test/{name}") public String test(@PathVariable String name){ return "hello,"+name; }
服務(wù)消費者
@GetMapping("/test/{name}") String test(@PathVariable("name") String name);
(2)GET——@RequestParam
服務(wù)提供者
@RequestMapping("/test") public String test(String name){return "hello,"+name; }
服務(wù)消費者
@RequestMapping("/test") String test(@RequestParam String name);
會遇到報錯
RequestParam.value() was empty on parameter 0
解決方法:
加上注解的描述,修改為
@RequestMapping("/test") String test(@RequestParam("name") String name);
(3)POST
@RequestBody
不需要注解的描述
@RequestMapping("/test") String test(@RequestBody String name);
注:
- 參數(shù)前使用了@RequestBody注解的,都以POST方式消費服務(wù)
- @RequestBody注解的參數(shù),需要POST方式才能傳遞數(shù)據(jù)
2.Feign多參數(shù)的問題
(1)GET——@PathVariable
服務(wù)提供者
@GetMapping("/test/{name}/{xyz}") public String test(@PathVariable String name,@PathVariable String xyz){ return "hello,"+name+","+xyz; }
服務(wù)消費者
@GetMapping("/test/{name}/{xyz}") String test(@PathVariable("name") String name,@PathVariable("xyz") String xyz);
(1)GET——@RequestParam
服務(wù)提供者
@RequestMapping("/test") public String test(String name,Integer type){ if(type==1){ return "hello,"+name; }else{ return "hello,provider-"+name; } }
服務(wù)消費者
@RequestMapping("/test") String test(String name, Integer type);
會遇到報錯Method has too many Body parameters
說明:
如果服務(wù)消費者傳過來參數(shù)時,全都用的是@RequestParam的話,那么服務(wù)提供者的Controller中對應(yīng)參數(shù)前可以寫@RequestParam,也可以不寫
服務(wù)消費者feign調(diào)用時,在所有參數(shù)前加上@RequestParam注解
正確的寫法
@RequestMapping("/test") String test(@RequestParam("name") String name, @RequestParam("type") Integer type);
(2)POST
如果接收方不變
服務(wù)消費者
@RequestMapping("/test") String test(@RequestBody String name, @RequestBody Integer type);
會遇到報錯Method has too many Body parameters
服務(wù)消費者為
@RequestMapping("/test") String test(@RequestBody String name, @RequestParam("type") Integer type);
name的值會為null
說明:
如果服務(wù)消費者傳過來參數(shù),有@RequestBody的話,那么服務(wù)提供者的Controller中對應(yīng)參數(shù)前必須要寫@RequestBody
正確的寫法
服務(wù)提供者
@RequestMapping("/test") public String test(@RequestBody String name, Integer type){ if(type==1){ return "hello,"+name; }else{ return "hello,provider-"+name; } }
服務(wù)消費者正確的寫法
@RequestMapping("/test") String test(@RequestBody String name, @RequestParam("type") Integer type);
可以接收到參數(shù)
總結(jié):
- 請求參數(shù)前加上注解@PathVariable、@RequestParam或@RequestBody修飾
- 可以有多個@RequestParam,但只能有不超過一個@RequestBody
- 使用@RequestParam注解時必須要在后面加上參數(shù)名
- @RequestBody用來修飾對象,但是既有@RequestBody也有@RequestParam,那么參數(shù)就要放在請求的url中,@RequestBody修飾的就要放在提交對象中
- 當參數(shù)比較復(fù)雜時,feign即使聲明為get請求也會強行使用post請求
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)庫連接_jdbc-odbc橋連接方式(詳解)
下面小編就為大家?guī)硪黄狫ava數(shù)據(jù)庫連接_jdbc-odbc橋連接方式(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-088個簡單部分開啟Java語言學(xué)習(xí)之路 附j(luò)ava學(xué)習(xí)書單
8個簡單部分開啟Java語言學(xué)習(xí)之路,附j(luò)ava學(xué)習(xí)書單,這篇文章主要向大家介紹了學(xué)習(xí)java語言的方向,感興趣的小伙伴們可以參考一下2016-09-09Spring Boot Admin管理監(jiān)控數(shù)據(jù)的方法
本篇文章主要介紹了Spring Boot Admin管理監(jiān)控數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12聊聊springboot靜態(tài)資源加載的規(guī)則
這篇文章主要介紹了springboot靜態(tài)資源加載的規(guī)則,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12java搭建ftp/sftp進行數(shù)據(jù)傳遞的全過程
ftp是一種文件傳輸協(xié)議,讓客戶端和服務(wù)端能夠互相傳遞文件,圖片等數(shù)據(jù),sftp也是一種文件傳輸協(xié)議,但是相比較而言要比ftp安全性更好些,但是也有缺點就是傳輸效率低2021-07-07Spring實戰(zhàn)之使用c:命名空間簡化配置操作示例
這篇文章主要介紹了Spring實戰(zhàn)之使用c:命名空間簡化配置操作,結(jié)合實例形式詳細分析了Spring使用c:命名空間簡化配置的相關(guān)接口與配置操作技巧,需要的朋友可以參考下2019-12-12Spring事務(wù)傳播中嵌套調(diào)用實現(xiàn)方法詳細介紹
Spring事務(wù)的本質(zhì)就是對數(shù)據(jù)庫事務(wù)的支持,沒有數(shù)據(jù)庫事務(wù),Spring是無法提供事務(wù)功能的。Spring只提供統(tǒng)一的事務(wù)管理接口,具體實現(xiàn)都是由數(shù)據(jù)庫自己實現(xiàn)的,Spring會在事務(wù)開始時,根據(jù)當前設(shè)置的隔離級別,調(diào)整數(shù)據(jù)庫的隔離級別,由此保持一致2022-11-11解決Springboot get請求是參數(shù)過長的情況
這篇文章主要介紹了解決Springboot get請求是參數(shù)過長的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09