![](/img/trans.png)
[英]How to access Spring REST API in JHipster with Spring RestTemplate
[英]How to access Spring REST API in JHipster with standalone
我需要在java sheduling獨立服務器中調用de jhipster rest服務。 但我不知道如何,我嘗試使用HttpClient庫並使用CredentialsProvider設置de用戶名和密碼,我無法登錄使用
HttpGet httpget = new HttpGet("http://localhost:8080/#Login");
但是當我嘗試獲取jason api休息時,我得到了未經授權的HTTP 401
我看到de Gatlin Test在Scala中進行生產,就像在模擬網絡瀏覽器一樣。
所以我在這里堆放,我將感謝任何可以給我一些建議的人。
這些是代碼
HttpHost targetHost = new HttpHost("localhost", 8080, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(DEFAULT_USER,DEFAULT_PASS));
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
client = HttpClientBuilder.create().build();
response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION), context);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Estatus Codee : " +statusCode);
String output;
在此呼叫中,具有退出狀態401
response = client.execute(new HttpGet(URL_PROMOTORES), context);
statusCode = response.getStatusLine().getStatusCode();
System.out.println("Estatus Codee : " +statusCode);
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
response.close();
client.close();
提前致謝。
我認為您不應該使用basicAuth,因為它用於HTTP基本認證(RFC 2617),這與JHipster在您的案例登錄/密碼格式編碼和會話中使用的方式不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.