异常描述
feign.FeignException: status 405 reading UserFeignClient#get0(User); content:
{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed", "exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/user"}
FeignClient定义
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User get0(Long userId);
}
解决方法
明明定义的是GET请求,结果在发起service to service call时,被转换成了POST请求。原因是OpenFeign在构造请求时需要足够多的@RequestMapping/@RequestParam/@PathVariable/@RequestHeader等来构造http请求。通过阅读自己定义的FeignClient可以发现,GET请求中的参数定义缺少注解@RequestParam,故而报错。 将参数userId明确定义为@RequestParam("userId") Long userId
关注公众号交流学习

关注Java分布式架构实战, 持续精进

联系我,学习交流

本文解析了Feign在发起service to service调用时,原本定义为GET请求被错误转换为POST请求的问题。深入探讨了OpenFeign构造请求的机制,并提供了正确的参数注解使用方法,以解决此异常。
581

被折叠的 条评论
为什么被折叠?



