zoukankan      html  css  js  c++  java
  • 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的限制,当然还会继续完善的

  • 相关阅读:
    JavaWeb--HttpSession案例
    codeforces B. Balls Game 解题报告
    hdu 1711 Number Sequence 解题报告
    codeforces B. Online Meeting 解题报告
    ZOJ 3706 Break Standard Weight 解题报告
    codeforces C. Magic Formulas 解题报告
    codeforces B. Sereja and Mirroring 解题报告
    zoj 1109 Language of FatMouse 解题报告
    hdu 1361.Parencodings 解题报告
    hdu 1004 Let the Balloon Rise 解题报告
  • 原文地址:https://www.cnblogs.com/fangh816/p/13427101.html
Copyright © 2011-2022 走看看