Beh*_*bur 4 java eclipse rest spring 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[]) {
SpringApplication.run(Client.class);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
RestTemplate restTemplate = restTemplateBuilder.build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
return restTemplate;
}
Run Code Online (Sandbox Code Playgroud)
您没有在maven项目依赖项中声明HttpClient,因此它将不在您的类路径中,因此在运行时不可用。
只需添加:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
到您的pom文件,然后它应该在classpath上可用。(您不必指定从Spring Boot父pom继承的版本)。
至于为什么您看到 sun.net.www.http.HttpClient它似乎只是错误地假定您的IDE类型,HttpClient因为它无法访问正确的IDE,因为它不在类路径中。
| 归档时间: |
|
| 查看次数: |
2985 次 |
| 最近记录: |