I am trying to create a rest client. I am using Spring. I have instantiated HttpComponentsClientHttpRequestFactory, which uses org.apache.http.client.HttpClient inside. Maven compilation is Ok, but when I run the project I get java.lang.ClassNotFoundException: org.apache.http.client.HttpClient.
Furthermore, when I place the mouse pointer on top of the import of HttpClient, I get sun.net.www.http.HttpClient, which is in rt.jar, instead of getting org.apache.http.client.HttpClient. I have checked that HttpClient truly does not exist in any other package. I know that I can find org.apache.http.client.HttpClient in other artifacts, such as
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0-alpha3</version>
However, I am not sure whether this should be right, since Spring itself does not seem to resort to this (I cannot see it does when I check the dependency hierarchy in the pom file).
In any case, I wonder why an import such as org.apache.http.client.HttpClient should resort to sun.net.www.http.HttpClient. I guess that the fact that the latter is in rt.jar is related to HttpClient not being available at run time.
If you want to check about all this, this is my pom file, it is quite simple:
<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>
Thanks a lot.
Edit:
The code is quite simple, you just need to instantiate the class HttpComponentsClientHttpRequestFactory. There is more code, but you only need this to obtain the exception.
@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;
}