首页 > 解决方案 > Apache HTTPClient 依赖问题:POM

问题描述


在我的 java 应用程序中使用 Apache HttpClient 时遇到问题。

2019-02-11 07:09:18,270 ERROR [Call-Dequeue-Delegator] (HibernateUtil.java:275) - Building SessionFactory failed.
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
    at java.lang.Class.getDeclaredMethods0(Native Method)

它在我的本地机器上运行良好,但在服务器上运行良好。由于它是一个客户端应用程序,我没有服务器等的详细信息。

以下是我的代码:

RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(20)
                .setConnectionRequestTimeout(30).build();

        // Creating client with request configuration(timeouts) & disabling redirect following
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).disableRedirectHandling().build();

        String endpoint = this.getSOAPEndPoint();
        String queryParamWithSurveyData = addDataToQueryParam(endpoint, customRequestModel);
        endpoint += queryParamWithSurveyData;
        HttpRequestBase httpRequestWithoutBody = null; // Failing at this

在上面的代码中,它在最后一行失败了,我很惊讶为什么不在第一行,因为(RequestConfig 和 HttpRequestBase)都是从 HTTPClient.jar 导入的

以下是我的 POM 片段

//Existing sample dependency
    <dependency> //Existing sample dependency
        <groupId>com.connectfirst.intelliqueue</groupId>
        <artifactId>gson</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/couchbase/gson-2.3.jar</systemPath>
        <version>1</version>
    </dependency>

// New dependencies added
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

我在某处看到 HttpClient 也依赖于其他一些 JAR,因此也添加了以下依赖项,但没有运气。

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/commons-codec-1.11.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/commons-logging-1.2.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>fluent-hc</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/fluent-hc-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-cache</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpclient-cache-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-osgi</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpclient-osgi-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-win</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpclient-win-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/httpmime-4.5.7.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/jna-4.5.2.jar</systemPath>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/jna-platform-4.5.2.jar</systemPath>
        <version>1</version>
    </dependency>

使用 maven clean install 在我的本地成功创建了 JAR。任何线索都会有所帮助。谢谢。

标签: javaspringmavenpom.xmlapache-httpclient-4.x

解决方案


你为什么要确定范围system?这意味着 jar 位于正在运行的应用程序的类路径上。如果将其切换为编译,假设您将打包为 spring-boot JAR 或 WAR 文件,它们将包含在生成的工件中。否则,应用程序无法找到您正在使用的依赖项。

scope属性指示应将依赖项放置在类路径上的位置。

  • compile - 工件可供项目代码参考,并且应该(表面上)与生成的工件捆绑在一起(例如嵌入到 WAR 中的 WEB-INF/lib 或 spring-boot 作为 BOOT-INF/lib 下)
  • 提供 - 工件可供项目源代码参考,但不包含在生成的工件中(想想 Web 应用程序中的 JSP 库,您想推迟到容器实现)。
  • 运行时 - 依赖类不可用于项目源代码直接引用,但在运行时引用(想想 JDBC 驱动程序,您不会专门使用它来加载它,传统上,通过使用名称的反射)。
  • test - 依赖类可用于 src/test/java (等)中的源。IDE 应该将引用分开,但并非总是如此(例如 Eclipse)。如果您尝试test从 src/main/java 引用范围依赖项,您的代码将无法编译,因为该依赖项不可用。
  • system - 在这种情况下,工件通过路径显式定位,并且预计在运行时在使用应用程序的类路径中可用。
  • import - 这仅在dependencyManagement 元素中受支持。

你可以从官方文档中看到描述。


推荐阅读