gpt4 book ai didi

java - Unirest 在使用 asString 方法时给出 NoSuchMethodError

转载 作者:行者123 更新时间:2023-11-30 10:01:44 25 4
gpt4 key购买 nike

我正在尝试简单地访问端点并使用 asString() 方法获取响应主体和 HttpResponse。代码可以编译,但在运行时会出错,说没有这样的方法错误。

我将 Unirest 导入到我的 Maven 构建中,并且我一直在关注文档 here

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 调用几乎是来自文档)。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.ideiio.testdataloader</groupId>
<artifactId>TestDataLoader</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>TestDataLoader</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.6</version>
</dependency>

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>2.3.11</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>

最佳答案

这是一个依赖问题,我修复了它,在 pom.xml 中添加了更新版本的 httpclient。 4.5.9 覆盖 4.5.6。

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>

关于java - Unirest 在使用 asString 方法时给出 NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57294615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com