我正在嘗試簡單地訪問端點并使用 asString() 方法獲取響應主體和 HttpResponse。代碼可以編譯,但在運行時會出錯,說沒有這樣的方法錯誤。我將 Unirest 導入到我的 Maven 構建中,并且我一直在關注這里的文檔HttpResponse<String> response = Unirest.get("http://mywebsite/post")
.basicAuth("myapiuser", "mypassword")
.asString();這是堆棧跟蹤:Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.client.config.RequestConfig$Builder.setNormalizeUri(Z)Lorg/apache/http/client/config/RequestConfig$Builder;
at kong.unirest.apache.DefaultFactory.tryNormalize(DefaultFactory.java:47)
at kong.unirest.apache.DefaultFactory.apply(DefaultFactory.java:42)
at kong.unirest.apache.DefaultFactory.apply(DefaultFactory.java:32)
at kong.unirest.apache.RequestPrep.getHttpRequestBase(RequestPrep.java:88)
at kong.unirest.apache.RequestPrep.prepare(RequestPrep.java:69)
at kong.unirest.apache.ApacheClient.request(ApacheClient.java:122)
at kong.unirest.BaseRequest.asString(BaseRequest.java:177)
at com.ideiio.testdataloader.TestDataLoader.testRestGet(TestDataLoader.java:62)
at com.ideiio.testdataloader.TestDataLoader.main(TestDataLoader.java:34)我無法確定這是否只是我缺少依賴項的問題(我對 Maven 很陌生)或者代碼本身是否有問題(盡管 Unirest 調用幾乎是文檔的精確副本) .
2 回答

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
這是一個依賴性問題,我修復了它,在 pom.xml 中添加了更新版本的 httpclient。4.5.9 覆蓋 4.5.6。
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.9</version> </dependency>

喵喔喔
TA貢獻1735條經驗 獲得超5個贊
查看您的異常,它正在尋找方法“java.lang.NoSuchMethodError: org.apache.http.client.config.RequestConfig$Builder.setNormalizeUri(Z)Lorg/apache/http/client/config/RequestConfig$Builder”,其中我可以看到方法簽名為“public RequestConfig.Builder setNormalizeUri(boolean normalizeUri)”這里的問題可能與使用的不兼容依賴項的版本有關,或者可能是由于類路徑中的其他 jar 引起的沖突??梢哉埛窒砟?pom.xml。
添加回答
舉報
0/150
提交
取消