HttpGet 类型未定义方法 addHeader (String, String)

the method addHeader (String, String) is undefined for the type HttpGet

我有这个程序:

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class ApplicationRESTFul {

    public static void main(String[] args) {

        String url = "http://www.google.com/search?q=httpClient";

        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);

        request.addHeader("Accept", "application/json");

    }

}

但我从 eclipse 收到了这条消息

the method addHeader (String, String) is undefined for the type HttpGet

我正在使用这个库,正如我在文档中看到的那样,该方法应该存在 (org.apache.httpcomponents.httpclient_4.5)

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpGet.html

从 maven 导入依赖而不是在类路径中添加 lib 解决了问题

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>  

我通过将 httpcore JAR 添加到 class 路径解决了这个问题。添加来自 maven 的依赖项也会添加 httpcore JAR 而不仅仅是 httpclient JAR,这就是它也有效的原因。