解決feignClient調(diào)用時獲取返回對象類型匹配的問題
feignClient調(diào)用時獲取返回對象類型匹配
feignClient是springCloud體系中重要的一個組件,用于微服務(wù)之間的相互調(diào)用,底層為httpClient,在之前的應(yīng)用中,我一直以為A服務(wù)提供的方法返回類型為對象的話,那么調(diào)用A服務(wù)的B服務(wù)必須也用字段類型以及命名完全相同的對象來接收,為此我驗證了一下,發(fā)現(xiàn)不是必須用完全相同的對象來接收,比如,可以用map<String,Object>或者Object來接收,然后解析。
當(dāng)然,復(fù)雜對象我還是推薦用一個完全相同的對象來接收。
下面是我的例子:
feignClient是springCloud體系中重要的一個組件,用于微服務(wù)之間的相互調(diào)用,底層為httpClient,在之前的應(yīng)用中,我一直以為A服務(wù)提供的方法返回類型為對象的話,那么調(diào)用A服務(wù)的B服務(wù)必須也用字段類型以及命名完全相同的對象來接收,為此我驗證了一下,發(fā)現(xiàn)不是必須用完全相同的對象來接收,比如,可以用map<String,Object>或者Object來接收,然后解析。
當(dāng)然,復(fù)雜對象我還是推薦用一個完全相同的對象來接收。
下面是我的例子:
項目一:首先創(chuàng)建一個服務(wù)注冊中心eureka

配置文件

項目二:緊接著創(chuàng)建一個服務(wù)提供方eureka-client,方法helloWorld返回值為map

注冊到eureka中

項目三:創(chuàng)建服務(wù)調(diào)用方service-feign


注意:map的key與Hello類字段屬性對應(yīng)

注冊到eureka

最后:啟動三個項目

發(fā)現(xiàn)項目二 項目三已經(jīng)被注冊到服務(wù)中心
調(diào)用項目三的接口

可以正常返回,由于項目二返回類型是map.而項目三是用對象Hello來接收的,那么就說明了服務(wù)提供方的返回值類型和調(diào)用方接收值類型并不是需要完全對應(yīng)的。
feignClient傳參(參數(shù)為對象類型)的一個坑
客戶端
@RequestMapping(value = "/friendCircleComment/comment",method = RequestMethod.POST) R comment(@RequestBody FriendCircleComment friendCircleComment);
服務(wù)端
@RequestMapping(value = "/comment")
public R comment(@RequestBody FriendCircleComment friendCircleComment){
friendCircleCommentService.comment(friendCircleComment);
return new R();
}
這么傳參是沒問題的,服務(wù)端也能接收到
但是,問題來了,
小程序的post請求的header必須為
header:{ 'content-type':'application/x-www-form-urlencoded' },
導(dǎo)致后臺為@RequestBody接收不到參數(shù),
feignClient默認參數(shù)請求類型是
header:{ 'content-type':'application/json' },
定義@RequestBody接收參數(shù)的headers類型必須為
header:{ 'content-type':'application/json' },
所以這樣就有沖突,feignClient和定義為'content-type':'application/x-www-form-urlencoded'的請求接口不能共用
解決方法
不使用對象接收,使用基本類型接收
如下
客戶端
@RequestMapping(value = "/friendCircleComment/comment",method = RequestMethod.POST)
R comment(@RequestParam(value = "friendCircleId",required = false)Integer friendCircleId,
@RequestParam(value = "memberId",required = false)Integer memberId,
@RequestParam(value = "parentId",required = false)Integer parentId,
@RequestParam(value = "comment",required = false)String comment,
@RequestParam(value = "replyMemberId",required = false)Integer replyMemberId);
服務(wù)端
@RequestMapping(value = "/comment")
public R comment(@RequestParam(value = "friendCircleId",required = false)Integer friendCircleId,
@RequestParam(value = "memberId",required = false)Integer memberId,
@RequestParam(value = "parentId",required = false)Integer parentId,
@RequestParam(value = "comment",required = false)String comment,
@RequestParam(value = "replyMemberId",required = false)Integer replyMemberId
){
FriendCircleComment friendCircleComment = new FriendCircleComment();
friendCircleComment.setFriendCircleId(friendCircleId);
friendCircleComment.setMemberId(memberId);
friendCircleComment.setParentId(parentId);
friendCircleComment.setComment(comment);
friendCircleComment.setReplyMemberId(replyMemberId);
friendCircleCommentService.comment(friendCircleComment);
return new R();
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot2.x結(jié)合Mabatis3.x下Hikari連接數(shù)據(jù)庫報超時錯誤
本文針對Springboot2.x與Mybatis3.x結(jié)合使用時,Hikari連接數(shù)據(jù)庫出現(xiàn)超時錯誤的問題進行了深入分析,并提供了一系列有效的解決方法,感興趣的可以了解一下2023-11-11
SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常
這篇文章主要介紹了SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式
這篇文章主要介紹了Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

