SpringCloud中的Feign遠程調用接口傳參失敗問題
Feign遠程調用接口傳參失敗
我在做一個微服務調用的時候出現(xiàn)了被調接口傳參失敗問題
Feign是通過http協(xié)議調用服務的,后來發(fā)現(xiàn)是因為Gep和Maping不一致,還有使用feign時要記得給實體類加無參構造注解
同時這些注解都盡量一致,不然微服務調bug很麻煩。
這是調用者
這是feign的client
這是被調者
Feign遠程調用的注意點
在使用feign的過程中遇到了一些問題,所以在這里做以下總結:
定義的做遠程調用的api接口
中的方法參數(shù)列表中的參數(shù)都必須都要打上@RequestParam(“value”) 注解**,否則調用會報405異常,這一點是和controller中不一樣的,controller中的方法只要參數(shù)名和前臺傳入的參數(shù)鍵名對應上就能自動綁定上參數(shù)
復雜類型用必須打上@RequestBody注解
service微服務中的Controller的參數(shù)綁定
如果參數(shù)列表中有復雜類型,請使用Post請求,使用Get請求會報Bad Request錯誤,且需要打上@RequestBody注解,而普通基本類型可以不用打上@RequestParam注解可自動綁定參數(shù)
如有其它問題,也歡迎補充,放一下代碼:
api:
@FeignClient("MS-ADMIN-SERVICE") public interface FixFeignService { ? ? @GetMapping("/fix") ? ? public List<FixInfo> findAll(); ? ? @PostMapping("/fix/add") ? ? public int insert(@RequestBody FixInfo fixInfo); ? ? @PostMapping("/fix/limitByParam") ? ? public LayUIPageBean limitByParam(@RequestBody FixInfo fixInfo, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit); ? ? @PostMapping("/fix/delByIds") ? ? public boolean delByIds(@RequestParam("ids[]") Long[] ids); ? ? @GetMapping("/fix/findById") ? ? public FixInfo findById(@RequestParam("id") Long id); ? ? @PostMapping("/fix/update") ? ? boolean update(@RequestBody FixInfo fixInfo); }
service微服務
@RestController @RequestMapping("/fix") @Slf4j public class FixInfoController { ? ? @Autowired ? ? private FixInfoService fixInfoService; ? ? @GetMapping("") ? ? public List<FixInfo> findAll(){ ? ? ? ? List<FixInfo> all = fixInfoService.findAll(); ? ? ? ? return all; ? ? } ? ? @PostMapping("/add") ? ? public int insert(@RequestBody FixInfo fixInfo){ ? ? ? ? return fixInfoService.insert(fixInfo); ? ? } ? ? @PostMapping("/limitByParam") ? ? public LayUIPageBean limitByParam(@RequestBody FixInfo fixInfo,Integer page,Integer limit){ ? ? ? ? LayUIPageBean layUIPageBean = new LayUIPageBean(); ? ? ? ? PageHelper.startPage(page,limit); ? ? ? ? List<FixInfo> all = fixInfoService.findByParam(fixInfo); ? ? ? ? PageInfo<FixInfo> pageInfo = new PageInfo<>(all); ? ? ? ? return layUIPageBean.setCount((int)pageInfo.getTotal()).setData(pageInfo.getList()); ? ? } ? ? @PostMapping("/delByIds") ? ? public boolean delByIds(@RequestParam("ids[]") Long[] ids){ ? ? ? ? //log.info("id"+ids[0]); ? ? ? ? boolean flag= fixInfoService.delByIds(ids); ? ? ? ? return flag; ? ? } ? ? @GetMapping("/findById") ? ? public FixInfo findById(Long id){ ? ? ? ? return fixInfoService.findById(id); ? ? } ? ? @PostMapping("/update") ? ? public boolean update(@RequestBody FixInfo fixInfo){ ? ? ? ?return fixInfoService.update(fixInfo); ? ? } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringMVC使用MultipartFile實現(xiàn)文件上傳
這篇文章主要為大家詳細介紹了SpringMVC使用MultipartFile實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Java中StringBuilder字符串類型的操作方法及API整理
Java中的StringBuffer類繼承于AbstractStringBuilder,用來創(chuàng)建非線程安全的字符串類型對象,下面即是對Java中StringBuilder字符串類型的操作方法及API整理2016-05-05fastjson全局日期序列化設置導致JSONField失效問題解決方案
這篇文章主要介紹了fastjson通過代碼指定全局序列化返回時間格式,導致使用JSONField注解標注屬性的特殊日期返回格式失效問題的解決方案2023-01-01Springboot如何基于assembly服務化實現(xiàn)打包
這篇文章主要介紹了Springboot如何基于assembly服務化實現(xiàn)打包,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06