异常描述
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分布式架构实战, 持续精进
联系我,学习交流