您好我正在尝试使用Spring resttemplate连接弹性搜索,但它提供异常说错误请求。下面是我的代码
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)
请我这是第一次这样做
答案 0 :(得分:1)
这不是授权问题。您的url
只是在最后错过了_search
端点,因此您收到的错误是HTTP 400。
final String url = "https://ff92ba95318093026b7a06180f2b2d19.us-east-1.aws.found.io:9243/jinkjobposts_dev_v1/jobPosts/_search";