2017-02-15 58 views
0

您好我正嘗試使用Spring resttemplate進行彈性搜索連接,但它給出異常說不好的請求。下面是我的代碼彈簧基本OAuth投擲不良請求異常

String plainCreds = "elasticsearch:*****"; 
      byte[] plainCredsBytes = plainCreds.getBytes(); 
      byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
      String base64Creds = new String(base64CredsBytes); 

      HttpHeaders headers = new HttpHeaders(); 
      headers.set("Authorization", "Basic " + base64Creds); 

      final String url = "https://ff92ba95318093026b7a06180f2b2d19.us-east-1.aws.found.io:9243/jinkjobposts_dev_v1/jobPosts"; 

      RestTemplate restTemplate = new RestTemplate(); 
      restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 

      HttpEntity<String> request = new HttpEntity<>(headers); 
      Log.d("location", "before exchange"); 
      ResponseEntity<JobPosts> response = restTemplate.exchange(url, HttpMethod.POST, request, JobPosts.class); 

      JobPosts jobPosts = response.getBody(); 

      Log.d("location", "after exchange"); 
      list = Arrays.asList(jobPosts); 

它給出了這樣的例外。

FATAL EXCEPTION: AsyncTask #2 
Process: in.thoughtsmith.jink, PID: 8902 
java.lang.RuntimeException: An error occured while executing doInBackground() 
at android.os.AsyncTask$3.done(AsyncTask.java:304) 
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: org.springframework.web.client.HttpClientErrorException: 400 Bad Request 
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76) 
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:524) 
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481) 
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439) 
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415) 
at in.thoughtsmith.jink.MapsActivity$SearchJob.doInBackground(MapsActivity.java:690) 
at in.thoughtsmith.jink.MapsActivity$SearchJob.doInBackground(MapsActivity.java:645) 
at android.os.AsyncTask$2.call(AsyncTask.java:292) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115)  
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590)  
at java.lang.Thread.run(Thread.java:818)  

請我,我是第一次

+1

我想你只是在URL末尾缺少一個'/ _search'。 – Val

+0

是的,謝謝我錯過了/ _search。你能幫我在這個http://stackoverflow.com/questions/42240927/how-to-search-via-json-in-elastic-search-using-spring-resttemplate-in-android/42247492#42247492 – FaisalAhmed

回答

1

這不是一個授權問題這樣做。您的url結尾處僅丟失_search端點,因此您收到錯誤的HTTP 400。

final String url = "https://ff92ba95318093026b7a06180f2b2d19.us-east-1.aws.found.io:9243/jinkjobposts_dev_v1/jobPosts/_search"; 
+0

謝謝瓦爾。我想連接到彈性搜索使用彈簧休息模板在這裏http://stackoverflow.com/questions/42240927/how-to-search-via-json-in-elastic-search-using-spring-resttemplate-in-但它沒有給我結果或例外 – FaisalAhmed