【发布时间】:2019-12-09 04:49:04
【问题描述】:
我正在尝试对端点进行简单的获取,并使用 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