Unirest 在使用 asString 方法时给出 NoSuchMethodError

Unirest gives NoSuchMethodError when using asString method

我正在尝试简单地访问端点并使用 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>

看着你的异常它正在寻找方法 "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) “ 这里的问题可能与使用的不兼容依赖项的版本有关,或者可能是由于 class 路径中的其他 jar 造成的冲突。可以请分享您的 pom.xml.

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

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