实际上,客户端驻留在tomcat 8080中,而REST API驻留在9090中。URL移至更高环境时将有所不同。我看不到使用httpasyncclient进行的REST API调用。我从Apache网站复制了代码
https://hc.apache.org/httpcomponents-asyncclient-dev/examples.html
不确定,即使我收到异常响应也如何使通话成功
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
try {
client.start();
JsonObject obj = new JsonObject();
obj.addProperty("username", "username");
obj.addProperty("password", "password");
String serverURL = "http://localhost:9090/project/api";
HttpPost postRequest = new HttpPost(serverURL);
StringEntity params = new StringEntity(obj.toString(), "UTF-8");
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "x-www-form-urlencoded"));
postRequest.setEntity(params);
Future<HttpResponse> future = client.execute(postRequest, null);
HttpResponse response = future.get();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我没有找到任何有用的信息来解决此问题。任何帮助都会让我开心
Caused by: java.lang.ArrayStoreException: org.apache.http.impl.cookie.RFC2965VersionAttributeHandler
at org.apache.http.impl.cookie.DefaultCookieSpecProvider.create(DefaultCookieSpecProvider.java:93)
at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:152)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:133)
at org.apache.http.impl.nio.client.MainClientExec.prepareRequest(MainClientExec.java:520)
at org.apache.http.impl.nio.client.MainClientExec.prepare(MainClientExec.java:146)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:124)
at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:74)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:107)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:91)
javadocs说
打开声明java.util.concurrent.ExecutionException异常
尝试检索中止的任务的结果时抛出
通过抛出异常。可以使用
getCause()方法。
最佳答案
最可能的原因-httpclient
版本中存在冲突。 F.e.项目依赖项包括httpclient
和httpasyncclient
。 httpasyncclient
对另一个版本的httpclient
具有传递依赖。结果,项目依赖项具有2个不兼容的httpclient
版本。此处将对其中一种情况进行详细说明:https://stackoverflow.com/a/49898063/4651234