https://blog.csdn.net/wandou9527/article/details/109751571

在项目中使用feign调用远程接口,有时对方是个get接口,而且有很多参数,写出来的feignclient就会有很多参数,我们很可能写出如下代码:

	@RequestMapping(path = "/s", method = RequestMethod.GET)
    String search(@RequestParam("param1") String param1,
    			  @RequestParam("param2") String param2,
    			  @RequestParam("param3") String param3,
    			  @RequestParam("paramn") String paramn,
    			  ...
    			  );

传递起来参数很不方便,那能不能用封装对象的形式传参呢? 答案是肯定的。

然后很多人写成如下方式:

	@RequestMapping(path = "/s", method = RequestMethod.GET)
    String searchV2(BaiduParamDTO param);

但运行发现是报错的,这可怎么办,本着问题要从根上找的原则,打开了 spring-cloud-openfeign 的官方文档。

https://docs.spring.io/spring-cloud-openfeign/docs/2.2.6.RELEASE/reference/html/#feign-querymap-support

文档中果然有关于这个功能的介绍

根据官方文档,我写成如下代码:

	@RequestMapping(path = "/s", method = RequestMethod.GET)
    String searchV2(@SpringQueryMap BaiduParamDTO param);

运行通过,ok,完美解决。


源码:https://github.com/wandou9527/spring-boot-feign