5

我一直在像这样使用 Java 的 CompletableFuture

CompletableFuture.runAsync(() -> {//Some code here });

当我尝试在这段代码中使用 Resteasy Client 时,我得到一个

javax.ws.rs.ProcessingException: Unable to find a MessageBodyReader of content-type application/json;charset=utf-8 and type class java.lang.String

如果我在 completablefuture 之外使用客户端,它就可以工作。Resteasy 代码看起来像这样

        ResteasyClient client = new ResteasyClientBuilder().build();
        client.register(new AcceptEncodingFilter("gzip"));
        ResteasyWebTarget target = client.target(exampleURL);

        target = target.queryParam("1", 1)
                .queryParam("2", "1")
                .queryParam("3", 3)
                .queryParam("4", 4)
                .queryParam("5", "5");

        Response response = target.request().get();
        resultString = response.readEntity(String.class);

我将在 completablefuture 之外运行 resteasy 代码来“修复”问题,但想了解为什么会发生这种情况。

CompletableFuture 中的 resteasy 代码如下所示:

CompletableFuture.runAsync(() -> {
            try {
                ResteasyClient client = new ResteasyClientBuilder().build();
                client.register(new AcceptEncodingFilter("gzip"));
                ResteasyWebTarget target = client.target("http://test.com");

                target = target.queryParam("1", "1")
                        .queryParam("2", "2")
                        .queryParam("3", "3")
                        .queryParam("4", "4")
                        .queryParam("5", "5");

                Response response = target.request().get();
                String resultString = response.readEntity(String.class);

                response.close();
                client.close();
            } catch (Exception e){
                e.printStackTrace();
            }
        });

CompletableFuture 之外的相同代码有效

4

0 回答 0