Springcloud中Feign傳遞參數(shù)的過程解析
傳遞單個(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+"沒有找到該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來進(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來傳遞普通參數(shù),使用@SpringQueryMap來傳遞對(duì)象
注:本人親測(cè)踩坑使用@RequestParam+@RequestBody的時(shí)候,出現(xià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)文章
JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼
這篇文章主要介紹了JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
Java代碼實(shí)現(xiàn)哈希表(google 公司的上機(jī)題)
這篇文章主要介紹了Java 哈希表詳解(google 公司的上機(jī)題),本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
springboot實(shí)現(xiàn)excel表格導(dǎo)出幾種常見方法
在日常的開發(fā)中避免不了操作Excel,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)excel表格導(dǎo)出的幾種常見方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
Linux中如何修改jar包里的文件類及配置等內(nèi)容詳解
在項(xiàng)目測(cè)試或者部署后,經(jīng)常會(huì)遇到需要更改一些配置文件或者java類,這篇文章主要介紹了Linux中如何修改jar包里的文件類及配置等內(nèi)容的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-12-12
Spring Boot 集成 Kafkad的實(shí)現(xiàn)示例
這篇文章主要介紹了Spring Boot 集成 Kafkad的示例,幫助大家更好的理解和學(xué)習(xí)使用Spring Boot框架,感興趣的朋友可以了解下2021-04-04
SpringBoot3實(shí)現(xiàn)webclient的通用方法詳解
Spring Boot WebClient 是 Spring Framework 5 中引入的一個(gè)新的響應(yīng)式 Web 客戶端,用于異步和響應(yīng)式地與外部服務(wù)進(jìn)行通信,下面我們就來看看SpringBoot3實(shí)現(xiàn)webclient的通用方法吧2024-04-04
mybatis的association傳遞參數(shù)問題示例
這篇文章主要介紹了mybatis的association傳遞參數(shù)問題,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Gradle相對(duì)于Maven有哪些優(yōu)點(diǎn)
這篇文章主要介紹了Gradle相對(duì)于Maven有哪些優(yōu)點(diǎn),幫助大家選擇合適的自動(dòng)構(gòu)建工具,更好的構(gòu)建項(xiàng)目,感興趣的朋友可以了解下2020-10-10

