我正在嘗試簡(jiǎn)單地訪問(wèn)端點(diǎn)并使用 asString() 方法獲取響應(yīng)主體和 HttpResponse。代碼可以編譯,但在運(yùn)行時(shí)會(huì)出錯(cuò),說(shuō)沒(méi)有這樣的方法錯(cuò)誤。我將 Unirest 導(dǎo)入到我的 Maven 構(gòu)建中,并且我一直在關(guān)注這里的文檔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)我無(wú)法確定這是否只是我缺少依賴項(xiàng)的問(wèn)題(我對(duì) Maven 很陌生)或者代碼本身是否有問(wèn)題(盡管 Unirest 調(diào)用幾乎是文檔的精確副本) .
2 回答

慕虎7371278
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是一個(gè)依賴性問(wèn)題,我修復(fù)了它,在 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貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
查看您的異常,它正在尋找方法“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)”這里的問(wèn)題可能與使用的不兼容依賴項(xiàng)的版本有關(guān),或者可能是由于類路徑中的其他 jar 引起的沖突??梢哉?qǐng)分享您的 pom.xml。
添加回答
舉報(bào)
0/150
提交
取消