小编Beh*_*bur的帖子

HttpClient在运行时不存在

我正在尝试创建一个休息客户。我正在使用Spring。我已经实例化了HttpComponentsClientHttpRequestFactory,里面使用了org.apache.http.client.HttpClient。Maven编译还可以,但是当我运行项目时,我得到了java.lang.ClassNotFoundException: org.apache.http.client.HttpClient.

此外,当我将鼠标指针放在HttpClient导入的顶部时,我sun.net.www.http.HttpClient在rt.jar中得到get ,而不是get org.apache.http.client.HttpClient。我检查了HttpClient在其他任何软件包中是否确实不存在。我知道我可以org.apache.http.client.HttpClient在其他工件中找到,例如

<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0-alpha3</version>
Run Code Online (Sandbox Code Playgroud)

但是,我不确定这是否正确,因为Spring本身似乎并不求助于此(检查pom文件中的依赖关系层次结构时,我看不到它是正确的)。

无论如何,我不知道为什么诸如之类的进口商品org.apache.http.client.HttpClient应诉诸于sun.net.www.http.HttpClient。我猜想后者在rt.jar中的事实与HttpClient在运行时不可用有关。如果要检查所有这些,这是我的pom文件,它非常简单:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xxx.xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.5.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

非常感谢。

编辑: 代码非常简单,您只需要实例化该类HttpComponentsClientHttpRequestFactory。还有更多代码,但是您只需要此代码即可获取异常。

@SpringBootApplication
public class Client {

    public static void main(String args[]) { …
Run Code Online (Sandbox Code Playgroud)

java eclipse rest spring httpclient

4
推荐指数
1
解决办法
2985
查看次数

标签 统计

eclipse ×1

httpclient ×1

java ×1

rest ×1

spring ×1