Feign消費服務時POST/GET請求方式


這篇博文主要為了解決大家平時Feign消費服務時POST/GET請求方式遇到的一些坑
在之前大家肯定需要熟悉Feign消費服務時POST/GET請求方式的一些用法,如果不知道可以直接點擊這篇博主的文章Feign消費服務時POST/GET請求方式
一定要看完這篇文章之后才能get到坑點:

類型List參數傳輸

首先我經過很多例子來測試,feign雖然吸收了很多mvc的用法習慣但是限制很是很多的,比如這個地方,在feign中無法直接傳輸List類型的

@GetMapping({"/sysOrgrole/bindResources"})
public JsonResult bindResources(@RequestParam("orgroleId") String orgroleId, @RequestParam
("resourceIds") String[] resourceIds);
此時是get請求,用數組接受,不能用List來請求,否則就是需要用@RequestBody()來解析

而在mvc中卻可以直接用List來接受的

springcloud feign傳輸List的坑

無法直接傳輸List

錯誤方法1:

    @RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST)
    @ResponseBody
    MerchantCompareTotalInfo getMerchantCompareInfo(
            @RequestParam(value = "licenseNoList")
            List<String> licenseNoList);

錯誤: feign.FeignException: status 500 reading MerchantStatRemoteApi#getMerchantCompareInfo(List); content

錯誤方法2:

   @RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST)
    @ResponseBody
    MerchantCompareTotalInfo getMerchantCompareInfo(@RequestBody List<String> licenseNoList);

錯誤: feign.FeignException: status 500 reading MerchantStatRemoteApi#getMerchantCompareInfo(List); content

錯誤方法3:

    @RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST)
    @ResponseBody
    MerchantCompareTotalInfo getMerchantCompareInfo(@RequestBody String[] licenseNoList);

服務端的數組是null

正確方法:

    @RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST)
    @ResponseBody
    MerchantCompareTotalInfo getMerchantCompareInfo(@RequestParam("licenseNoList") String[] licenseNoList);

當然你也可以不像上面的處理方式,采用mvc另一種用法就是把List放在bean對象中如果加上@RequestParam也是可以的

restful方式

    /**
     * 單位角色刪除(邏輯)
     *
     * @param id
     * @return
     */
    @PostMapping({"/sysOrgrole/logicDelete/{id}"})
    JsonResult logicDelete(@PathVariable("id") String id);

此時就不需要加@RequestParam注解了

如果是get請求但是參數是bean的傳輸方式

    /**
     * 分頁查詢
     * @param dutyQueryRequest
     * @return
     */
    @RequestMapping(value = "/dutyManage/findPage", method = RequestMethod.GET)
    JsonResult<PageInfo<DutyResponse>> findPage(@SpringQueryMap DutyQueryRequest dutyQueryRequest,
                                                @RequestParam("pageNum") Integer pageNum,
                                                @RequestParam("pageSize") Integer pageSize);

截止目前為止我測試了很多demo來驗證發現這些feign的限制,當然還會繼續完善的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM