天天看點

Feign異常--Request method ‘POST‘ not supported

文章目錄

  • ​​1、前置​​
  • ​​2、出現該問題的方式​​
  • ​​3、出現的問題​​
  • ​​4、通過源碼進行分析​​
  • ​​5、解決方法​​
  • ​​6、說明, 個人了解, 歡迎指正​​
  • ​​7、完結​​

1、前置

1、這個問題我也找了很久, 也從網上搜尋了很多方法, 會在最後列出對應的位址

2、可能版本不同, 觸發該問題的機制也會不一樣, 因為我的解決方法和網上羅列的不是完全一樣

3、我的版本:

**

Spring Cloud: Hoxton.SR3

spring-cloud-openfeign: 2.2.2.RELEASE

2、出現該問題的方式

最近在整理feign的各種調用方式, 本來準備先寫調用方式這篇文章的, 結果發現了這個問題, 給先記錄下來

/**
     * GET 請求多參, 攜帶頭部
     *
     * @param data
     * @param headerMap
     * @return
     */
    @RequestLine("GET /testHeard/getHeard")
    CommonResult getHeard(@SpringQueryMap TestEntity data, @HeaderMap Map<String, Object> headerMap);
      

3、出現的問題

進行服務調用的時候, 傳回了一個錯誤, 請求方法錯誤, 不支援POST. 因為我服務提供者是一個GET請求.

Feign的日志是先列印的, 然後在做請求操作

Feign異常--Request method ‘POST‘ not supported

4、通過源碼進行分析

Feign異常--Request method ‘POST‘ not supported

至于為什麼會強制轉換, 這個大家可以自行研究一下, HttpURLConnection設定get請求無效.

我找尋了一下JDK8的文檔, 并沒有描述說會出現這種情況:https://docs.oracle.com/javase/8/docs/api/

但是Android的文檔裡面卻對此進行了描述:

https://developer.android.com/reference/java/net/HttpURLConnection.html

Feign異常--Request method ‘POST‘ not supported

然後就是get請求request.body() != null為何成立, 這個就沒有細看了, 感覺弄不太明白, 就不寫了

--------------------------分割線------------------------------

當我不使用@RequestLine注解時, 這裡就是正常的了, 也算是一種解決方案

Feign異常--Request method ‘POST‘ not supported

5、解決方法

我使用的這個版本, 隻需要這一步即可. 替換feign的httpclient

<!-- 使用Apache HttpClient替換Feign原生httpclient -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>10.1.0</version>
</dependency>
      

網上看到有的說需要配置連接配接方式, 在我這個版本可以不用做操作, 預設就是true

​​文檔位址​​:https://docs.spring.io/spring-cloud-openfeign/docs/2.2.x-SNAPSHOT/reference/html/appendix.html

Feign異常--Request method ‘POST‘ not supported
# 我使用的這個版本這一步可以忽略
   httpclient:
    enabled: true
      

6、說明, 個人了解, 歡迎指正

當我們引入feign-httpclient後:(feign-httpclient用的是ApacheHttpClient)

同時他也實作了feign的Client

Feign異常--Request method ‘POST‘ not supported
Feign異常--Request method ‘POST‘ not supported

繼續斷點:

Feign異常--Request method ‘POST‘ not supported

7、完結

1、從官網尋找自己的版本: https://docs.spring.io/spring-cloud-openfeign/docs/, 優先從官方查詢是否有對應的解決方法