gpt4 book ai didi

java - 如何保存 Vert.x WebClient 结果并在方法中返回它

转载 作者:行者123 更新时间:2023-11-30 06:02:52 25 4
gpt4 key购买 nike

我有一个在我的 Java Vert.x 项目中使用的方法,它使用 WebClient 调用另一个服务。我想将该调用的结果保存在 JsonObject 中并返回它。我假设我需要为此使用 futures,但是当我运行 GetItemsService.retrieveItems 时,除了我在控制台中看到的“打印 future ”消息外,我在控制台中看到的是 Future{unresolved

如何将 GetItemsManager.getItems Web 客户端调用的结果保存在变量中?

public class GetItemsManager {
public Future<JsonObject> getItems(Future<JsonObject> future, Vertx vertx, RoutingContext routingContext) {
WebClient webClient = WebClient.create(vertx);
HttpServerResponse response = routingContext.response();
JsonObject data = new JsonObject();
response.setChunked(true);
response.putHeader("content-type", "text/plain");
response.putHeader("Access-Control-Allow-Origin", "*");
webClient.post(80, Constants.API_URL, "/items")
.as(BodyCodec.jsonArray())
.putHeader("Accept", "application/json")
.putHeader("Content-Type", "application/json")
.putHeader("ugws7", Constants.SERVICES_URL)
.sendJsonObject(new JsonObject().put("mutator", "*"), ar -> {
if (ar.succeeded()) {
HttpResponse<JsonArray> result = ar.result();
JsonArray body = result.body();
data.put("data", body.getJsonObject(0));
future.complete(data);
} else {
future.fail(new JsonObject().put("error", ar.cause().getMessage()).encode());
}
});
return future;
}
}

public class GetItemsService {
public void retrieveItems(Vertx vertx, RoutingContext routingContext) {
Future<JsonObject> future = Future.future();
new GetItemsManager().getItems(future, vertx, routingContext);
String jsonObject = future.toString();
System.out.println("print future below");
System.out.println(jsonObject);
}
}

更新

根据 Peter 的回答,我尝试用以下内容更新我的代码:

   public class GetItemsService {
public void retrieveItems(Vertx vertx, RoutingContext routingContext) {
Future<JsonObject> future = Future.future();
new GeItemsManager().getItems(future, vertx, routingContext);
String jsonObject;
Future<JsonObject> f = future.setHandler(handler -> {
handler.result();
});
System.out.println("Print future below");
System.out.println(f);
}
}

我仍然无法将调用结果分配给变量 (f)。

最佳答案

我希望这有助于我尝试向您的代码添加有用的注释。

获取元素管理器

public class GetItemsManager {
public Future<JsonObject> getItems(Vertx vertx, RoutingContext routingContext) {
//Note1: you can create the future here, no need to pass as a parameter
Future<JsonObject> future = Future.future();
...
webClient.post(80, Constants.API_URL, "/items")
...
.sendJsonObject(new JsonObject().put("mutator", "*"), ar -> {
if (ar.succeeded()) {
...
future.complete(data);
} else {
future.fail(new JsonObject().put("error", ar.cause().getMessage()).encode());
}
});
return future;
}
}

获取元素服务

public class GetItemsService {
public void retrieveItems(Vertx vertx, RoutingContext routingContext) {
new GetLocationsManager().getItems(vertx, routingContext) // returns Future<JsonObject>
.setHandler(handler -> {
//Note2: this codeblock is executed when your future is completed (or failed)
if(handler.succeeded()){
//Note3: handler.result() contains the result from the getItems call (future.complete(data))
JsonObject items = handler.result();
doSomethingWithTheItems(items);
String itemsEncodedToString = items.encode();
// serve the response
routingContext.response().end(itemsEncodedToString);
}else{
// serve error response
routingContext.response().end("something terrible happened" + handler.cause());
}
});
}

public void doSomethingWithTheItems(JsonObject items){
// do something here with your items
}
}

//如果你想执行一些其他的异步方法,选项 2

public class GetItemsService {
public void retrieveItems(Vertx vertx, RoutingContext routingContext) {
new GetLocationsManager().getItems(vertx, routingContext);
.compose(this::doSomethingAsyncWithTheItems)
.setHandler(handler -> {
if(handler.succeeded()){
//Note4: handler.result() contains the result from the doSomethingAsyncWithTheItems call (future.complete(...))
JsonObject didSomethingWithTheItems = handler.result();
routingContext.response().end(didSomethingWithTheItems.encode());
}else{
// serve error response
routingContext.response().end("something terrible happened");
}
});
}

public Future<JsonObject> doSomethingAsyncWithTheItems(JsonObject items){
Future<JsonObject> future = Future.future();
otherAsyncCallThatReturnsAFuture.setHandler(handler -> {
if(handler.succeeded()){
future.complete(...)
}else{
future.fail(...)
}
})
return future;
}
}

关于java - 如何保存 Vert.x WebClient 结果并在方法中返回它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415119/

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