spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading

原创 2019-07-24 13:14 阅读(1391)次

微服务中使用feign内部调用报错:

Caused by: feign.FeignException$MethodNotAllowed: status 405 reading VisitorFeignClient#login(String)
at feign.FeignException.errorStatus(FeignException.java:100)
at feign.FeignException.errorStatus(FeignException.java:86)
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:93)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:149)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:109)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
... 26 more

feign接口:

    @GetMapping("/visitors/login")
    @ResponseBody
    public ResponseData<Visitor> login(String mobile);
目标微服务controller:

    @GetMapping("/login")
    @ResponseBody
    public ResponseData<Visitor> login(String mobile){
    	Visitor visitor = visitorService.login(mobile);
    	return ResponseUtils.returnSuccess("查询成功", visitor);
    }
解决过程:

我调试了一下feign原代码,就是上面报错的
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
这行代码,发现feign发送的请求体如下:

GET /visitors/login HTTP/1.1
Content-Length: 19
Content-Type: text/plain;charset=UTF-8

1563948734613
feign把接口上的mobile参数写到了body里,而GET请求是没有body的,所以这个feign接口一直报405错误。

所以需要在feign接口的参数上加上@RequestParam注解就可以了

    @GetMapping("/visitors/login")
    @ResponseBody
    public ResponseData<Visitor> login(@RequestParam String mobile);
再次查看请求体:

GET /visitors/login?mobile=%E6%B8%B8%E5%AE%A21563949032160 HTTP/1.1

Binary data
正常了。