Apache HTTPClient依赖问题:POM

问题描述 投票:0回答:1

我在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。任何领导都会有所帮助。谢谢。

java spring maven pom.xml apache-httpclient-4.x
1个回答
0
投票

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

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

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

你可以从official documentation看到描述。

© www.soinside.com 2019 - 2024. All rights reserved.