环境准备:
- JDK 1.8
- SpringBoot2.2.3
- SpringCloud(Hoxton.SR7)
- Maven 3.2
- 开发工具
- IntelliJ IDEA
- smartGit
maven加上openFeign配置
代码语言:javascript复制<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在使用Spring Cloud集成的openFeign GET请求调用一些接口,有时候会遇到需要传比较多的参数,所以,我们可以定义一个Bean类,直接丢过去?
代码语言:javascript复制@FeignClient(value = "EUREKA-SERVICE-PROVIDER" ,contextId = "EUREKA-SERVICE-PROVIDER")
@Service
public interface UserService {
@GetMapping(value = "/api/users")
List<User> list(User user);
}
调用一些服务接口,会出现报错,如图
代码语言:javascript复制feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://127.0.0.1:8083/api/users] [UserService#list(User)]: [{"timestamp":"2022-10-11T00:15:10.514 00:00","status":405,"error":"Method Not Allowed","message":"","path":"/api/users"}]
at feign.FeignException.clientErrorStatus(FeignException.java:203)
at feign.FeignException.errorStatus(FeignException.java:177)
at feign.FeignException.errorStatus(FeignException.java:169)
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92)
at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
at com.sun.proxy.$Proxy76.list(Unknown Source)
at com.example.springcloud.feign.SpringcloudFeignApplication.list(SpringcloudFeignApplication.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
所以,我们有什么方法?在spring-cloud-openfeign-core:2.1.0.RELEASE
之前版本是不可以支持Bean类直接丢的,所以,只能使用@RequestParam
传参数,在spring-cloud-openfeign-core:2.1.0.RELEASE
以及之后版本,可以使用提供的@SpringQueryMap
注解传一个bean对象过去
@FeignClient(value = "EUREKA-SERVICE-PROVIDER" ,contextId = "EUREKA-SERVICE-PROVIDER" )
@Service
public interface UserService {
@GetMapping(value = "/api/users")
List<User> list(@SpringQueryMap User user);
}