1.问题描述
在项目中使用feign调用远程接口,有时对方是个get接口,但是需要传递对象参数时会调用失败
@GetMapping ("certificateDataMiddle/getList")
R queryList(@RequestBody CertificateDataMiddle certificateDataMiddle);
2. 分析
查看openfeign文档可以发现传递多个参数时需要在参数前添加@SpringQueryMap注解
1.11. Feign @QueryMap support
The OpenFeign @QueryMap
annotation provides support for POJOs to be used as GET parameter maps. Unfortunately, the default OpenFeign QueryMap annotation is incompatible with Spring because it lacks a value
property.
Spring Cloud OpenFeign provides an equivalent @SpringQueryMap
annotation, which is used to annotate a POJO or Map parameter as a query parameter map.
For example, the Params
class defines parameters param1
and param2
:
// Params.java
public class Params {
private String param1;
private String param2;
// [Getters and setters omitted for brevity]
}
The following feign client uses the Params
class by using the @SpringQueryMap
annotation:
@FeignClient("demo")
public interface DemoTemplate {
@GetMapping(path = "/demo")
String demoEndpoint(@SpringQueryMap Params params);
}
If you need more control over the generated query parameter map, you can implement a custom QueryMapEncoder
bean.
3.解决
修改完之后代码如下
@GetMapping ("certificateDataMiddle/getList")
R queryList(@SpringQueryMap CertificateDataMiddle certificateDataMiddle);
现在就可以调通了