在springcloud项目中,基于fegin调用服务,结果出现类似如下异常:

status 405 reading EmpService#findAll(Integer); content: {"timestamp":"2019-06-02T08:00:49.203+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/emp/list"}

导致该异常出现的主要原因是consumer方法传递的参数,provider无法解析。

比如,consumer代码如下:

@Component
@FeignClient(value = "CLOUD-SERVICE-BASE")
public interface CityFeignService {

    /**
     * 根据城市名称查询城市信息
     *
     * @param name 城市名称
     * @return 城市信息
     */
    @GetMapping(value = "/city/getCityByName")
    CommonResult<CityVo> getCityByName(String name);
}

而provider中的代码如下:

    @GetMapping("/getCityByName")
    public CommonResult<CityVo> getCityByName(String name) {}

此时容易出现问题的地方有两个:第一个请求方式不一致,比如provider定义的为Get请求,而consumer采用的是post请求,则会出现异常。

而引起本文的异常原因主要是参数导致的。OpenFeign在构造请求时需要足@RequestMapping/@RequestParam/@PathVariable/@RequestHeader等来构造http请求。

而上述示例中name参数并未使用对应的注解,因此抛出异常。这里采用@RequestParam注解即可解决问题。修改之后的代码如下:

GetMapping(value = "/city/getCityByName")
CommonResult<CityVo> getCityByName(@RequestParam("name") String name);


fegin调用异常feign.FeignException$MethodNotAllowed: [405] during [GET] to插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.choupangxia.com/2021/01/20/feign-exception/