基本認証をrestTemplateに追加しようとしています
私が遭遇する問題は、初期化できないことです:(コードスニペットの両方のインポートで)
HttpClient client = new HttpClient();
このコードはコンパイル エラーで解決されます (この問題を解決するための Eclipse からの提案はありません)。
1) 問題は何ですか?
2) 間違ったクラスをインポートしていますか?
私のコードスニペット:
import org.apache.http.client.HttpClient;
//OR (not together)
import sun.net.www.http.HttpClient;
HttpClient client = new HttpClient(); //this line dosent compile
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("USERNAME","PASS");
client.getState().setCredentials(
  new AuthScope("www.example.com", 9090, AuthScope.ANY_REALM),
  credentials);
CommonsClientHttpRequestFactory commons =
     new CommonsClientHttpRequestFactory(client);
RestTemplate template = new RestTemplate(commons);
SomeObject result = template.getForObject(
     "http://www.example.com:9090/",SomeObject.class
 );
これを実行すると、例外が発生します:
> failed due to an unhandled exception: java.lang.Error: Unresolved
> compilation problems:     The constructor HttpClient() is not visible
>   The method getState() is undefined for the type HttpClient
>   CommonsClientHttpRequestFactory cannot be resolved to a type
>   CommonsClientHttpRequestFactory cannot be resolved to a type
>   SomeObject cannot be resolved to a type     The method
> getForObject(String, Class<SomeObject>, Object...) from the type
> RestTemplate refers to the missing type SomeObject    SomeObject cannot
> be resolved to a type