代码信息
本篇文章涉及代码版本
组件 | 版本 |
---|---|
Spring Boot | 2.0.8.RELEASE |
Spring Cloud | Finchley.SR1 |
整合feign时遇见的坑
本来计划中是没有这篇内容的,但是实际中这一个月来,整理了之前学习springcloud的知识和内容。因为之前有经验还算很快,但是在整合Feign的时候因为是一个不算太麻烦的功能所以就没去找以前的笔记,后来实际整合的时候各种坑大大超过了计划时间,所以决定把这些东西都整理出来。
错误:Request method ‘POST’ not supported
一个比较坑的问题
问题复现
当我们有一些下面这样的请求需要去远程应用拿数据的时候。
@RequestMapping(value = "user",method = RequestMethod.GET)
String getUserInfo(User user);
@RequestMapping(value = "getNumber",method = RequestMethod.GET)
String getNumber(Integer number);
@RequestMapping(value = "getStr",method = RequestMethod.GET)
String getStr(String str);
这个时候发起请求的时候,会出现这个异常
{"timestamp":"2019-07-13T08:52:13.330+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/user"}] with root cause
feign.FeignException: status 405 reading UserService#getUserInfo(User); content:
{"timestamp":"2019-07-13T08:52:13.330+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/user"}
异常原因
虽然我们使用的是GET方法,而服务提供方使用也是GET方法但是服务却告知我们缺失POST方法。这是因为只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求
解决办法
此时当数据是复杂对象的时候需要使用POST发起请求。
错误:PathVariable annotation was empty on param 0
问题复现
假如有个服务应用提供了这个请求接口
@RequestMapping(value = "/simple/{id}",method = RequestMethod.GET)
public String getId(@PathVariable(required = false) Long id) {
log.info("getId:{}",id);
return String.valueOf(id);
}
拿到相关接口规范后,尝试这么编写Feign的接口
@RequestMapping(value = "/simple/{id}",method = RequestMethod.GET)
String getId(@PathVariable(required = false) Long id);
@GetMapping(value = "/simple/{id}")
String getIdV2(@PathVariable(required = false) Long id);
此时却发现项目无法启动了并且抛出了下面的错误
Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
at feign.Util.checkState(Util.java:128) ~[feign-core-9.5.1.jar:na]
at org.springframework.cloud.openfeign.annotation.PathVariableParameterProcessor.processArgument(PathVariableParameterProcessor.java:51) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.cloud.openfeign.support.SpringMvcContract.processAnnotationsOnParameter(SpringMvcContract.java:238) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:110) ~[feign-core-9.5.1.jar:na]
at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66) ~[feign-core-9.5.1.jar:na]
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.5.1.jar:na]
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.5.1.jar:na]
at feign.Feign$Builder.target(Feign.java:218) ~[feign-core-9.5.1.jar:na]
at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:39) ~[spring-cloud-openfeign-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at o