1

实际上,客户端驻留在 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() 方法检查此异常。

4

1 回答 1

0

最可能的原因 -httpclient版本冲突。Fe 项目依赖包括httpclienthttpasyncclient. httpasyncclient对另一个版本的httpclient. 结果项目依赖项有 2 个不兼容的httpclient. 此处描述了一种带有详细解释的案例:https ://stackoverflow.com/a/49898063/4651234

于 2018-04-18T11:13:19.690 回答