SpringCloud Feign參數(shù)問(wèn)題及解決方法
這篇文章主要介紹了SpringCloud Feign參數(shù)問(wèn)題及解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
今天遇到使用Feign調(diào)用微服務(wù),傳遞參數(shù)時(shí)遇到幾個(gè)問(wèn)題
1.無(wú)參數(shù)
以GET方式請(qǐng)求
服務(wù)提供者
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
}
服務(wù)消費(fèi)者
@GetMapping("/hello")
String hello();
2.單個(gè)參數(shù)
(1)GET——@PathVariable
服務(wù)提供者
@GetMapping("/test/{name}")
public String test(@PathVariable String name){
return "hello,"+name;
}
服務(wù)消費(fèi)者
@GetMapping("/test/{name}")
String test(@PathVariable("name") String name);
(2)GET——@RequestParam
服務(wù)提供者
@RequestMapping("/test")
public String test(String name){return "hello,"+name;
}
服務(wù)消費(fèi)者
@RequestMapping("/test")
String test(@RequestParam String name);
會(huì)遇到報(bào)錯(cuò)
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方式消費(fèi)服務(wù)
- @RequestBody注解的參數(shù),需要POST方式才能傳遞數(shù)據(jù)
2.Feign多參數(shù)的問(wèn)題
(1)GET——@PathVariable
服務(wù)提供者
@GetMapping("/test/{name}/{xyz}")
public String test(@PathVariable String name,@PathVariable String xyz){
return "hello,"+name+","+xyz;
}
服務(wù)消費(fèi)者
@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ù)消費(fèi)者
@RequestMapping("/test")
String test(String name, Integer type);
會(huì)遇到報(bào)錯(cuò)Method has too many Body parameters
說(shuō)明:
如果服務(wù)消費(fèi)者傳過(guò)來(lái)參數(shù)時(shí),全都用的是@RequestParam的話,那么服務(wù)提供者的Controller中對(duì)應(yīng)參數(shù)前可以寫@RequestParam,也可以不寫
服務(wù)消費(fèi)者feign調(diào)用時(shí),在所有參數(shù)前加上@RequestParam注解
正確的寫法
@RequestMapping("/test")
String test(@RequestParam("name") String name, @RequestParam("type") Integer type);
(2)POST
如果接收方不變
服務(wù)消費(fèi)者
@RequestMapping("/test")
String test(@RequestBody String name, @RequestBody Integer type);
會(huì)遇到報(bào)錯(cuò)Method has too many Body parameters
服務(wù)消費(fèi)者為
@RequestMapping("/test")
String test(@RequestBody String name, @RequestParam("type") Integer type);
name的值會(huì)為null
說(shuō)明:
如果服務(wù)消費(fèi)者傳過(guò)來(lái)參數(shù),有@RequestBody的話,那么服務(wù)提供者的Controller中對(duì)應(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ù)消費(fèi)者正確的寫法
@RequestMapping("/test")
String test(@RequestBody String name, @RequestParam("type") Integer type);
可以接收到參數(shù)
總結(jié):
- 請(qǐng)求參數(shù)前加上注解@PathVariable、@RequestParam或@RequestBody修飾
- 可以有多個(gè)@RequestParam,但只能有不超過(guò)一個(gè)@RequestBody
- 使用@RequestParam注解時(shí)必須要在后面加上參數(shù)名
- @RequestBody用來(lái)修飾對(duì)象,但是既有@RequestBody也有@RequestParam,那么參數(shù)就要放在請(qǐng)求的url中,@RequestBody修飾的就要放在提交對(duì)象中
- 當(dāng)參數(shù)比較復(fù)雜時(shí),feign即使聲明為get請(qǐng)求也會(huì)強(qiáng)行使用post請(qǐng)求
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springBoot+mybatis-plus實(shí)現(xiàn)監(jiān)聽mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)增刪改
mybatis-plus技術(shù)是簡(jiǎn)化了繁瑣的代碼操作,把增刪改查的語(yǔ)句都內(nèi)置了,直接調(diào)用就可以實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查了,這篇文章主要給大家介紹了關(guān)于springBoot+mybatis-plus實(shí)現(xiàn)監(jiān)聽mysql數(shù)據(jù)庫(kù)數(shù)據(jù)增刪改的相關(guān)資料,需要的朋友可以參考下2024-01-01
Spring Boot通過(guò)Redis實(shí)現(xiàn)防止重復(fù)提交
表單提交是一個(gè)非常常見的功能,如果不加控制,容易因?yàn)橛脩舻恼`操作或網(wǎng)絡(luò)延遲導(dǎo)致同一請(qǐng)求被發(fā)送多次,本文主要介紹了Spring Boot通過(guò)Redis實(shí)現(xiàn)防止重復(fù)提交,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
netty?pipeline中的inbound和outbound事件傳播分析
這篇文章主要為大家介紹了netty?pipeline中的inbound和outbound事件傳播分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Spring-AOP @AspectJ進(jìn)階之如何綁定代理對(duì)象
這篇文章主要介紹了Spring-AOP @AspectJ進(jìn)階之如何綁定代理對(duì)象的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring webflux自定義netty 參數(shù)解析
這篇文章主要介紹了spring webflux自定義netty 參數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實(shí)URL的示例代碼
本篇文章主要介紹了Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實(shí)URL的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

