[英]Basic Authentication with RestTemplate - compilation error - The constructor HttpClient() is not visible
試圖將基本身份驗證添加到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
最后使用以下方法更容易運行基本身份驗證:
restTemplate.exchange()
並不是
restTemplate.getForObject()
我的代碼片段:
private HttpHeaders createHeaders(final String username, final String password ){
HttpHeaders headers = new HttpHeaders(){
{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic " + new String( encodedAuth );
set( "Authorization", authHeader );
}
};
headers.add("Content-Type", "application/xml");
headers.add("Accept", "application/xml");
return headers;
}
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<MyClass> response;
httpHeaders = this.createHeaders("user", "pass");
String url = "www.example.com"
response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(httpHeaders), MyClass.class);
它的工作原理!
如果使用Apache HttpClient,則實例化DefaultHttpClient
,因為HttpClient
只是一個接口:
HttpClient httpclient = new DefaultHttpClient();
請參閱此處有關使用HttpClient
各種身份驗證方案的文檔。
我們可以通過以下方式在Spring啟動中使用它:
@SpringBootApplication
public class Application實現CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
try{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders = this.createHeaders();
ResponseEntity<String> response;
response = restTemplate.exchange("<url>",HttpMethod.GET,new HttpEntity<Object>(httpHeaders),String.class);
log.info(response.toString());
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
private HttpHeaders createHeaders(){
HttpHeaders headers = new HttpHeaders(){
{
set( "Authorization", "3ee140");
}
};
return headers;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.