gpt4 book ai didi

android - Spring Basic OAuth抛出错误的请求异常

转载 作者:行者123 更新时间:2023-12-03 01:48:22 24 4
gpt4 key购买 nike

嗨,我正在尝试使用Spring resttemplate连接 Elasticsearch ,但是它给出了异常的错误请求。下面是我的代码

 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);

它给出了这个异常(exception)。
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) 

拜托,我是我第一次这样做

最佳答案

这不是授权问题。您的url只是在末尾缺少_search端点,因此出现了HTTP 400错误。

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

关于android - Spring Basic OAuth抛出错误的请求异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com