gpt4 book ai didi

java - 编译错误 : Cannot us a method returning CompetionStage as a Handler for requests

转载 作者:行者123 更新时间:2023-11-29 09:28:17 26 4
gpt4 key购买 nike

我在处理必须执行异步任务的操作时遇到了问题。该操作返回一个 CompletionStage,这样服务器就不会被异步任务(doSearch() 方法)阻塞,当任务完成时,我可以调用 complete() 来响应客户端。这是 controllers/Application.java 中的操作:

@BodyParser.Of(BodyParser.Json.class)
public static CompletionStage<Result> search(String secret) {
CompletableFuture<Result> result = new CompletableFuture<>();
if (!hasPermission(secret)) {
result.complete(unauthorized("Secret is incorrect."));
return result;
}
JsonNode json = request().body().asJson();
if (json == null) {
result.complete(badRequest("Expecting json data."));
return result;
}

SearchRequestBody search = Json.fromJson(json, SearchRequestBody.class);
doSearch(search.body).thenApply(searchResults -> {
result.complete(ok(Json.toJson(searchResults)));
return null;
});
return result;
}

路线文件:

# Home page
GET / controllers.Application.index

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

#PUT /predict/:id controllers.Application.predict()

GET /search controllers.Application.search

错误:

[info] Compiling 1 Java source to C:\Users\senok\IdeaProjects\Culinars\target\scala-2.11\classes...
[info] Compiling 1 Scala source to C:\Users\senok\IdeaProjects\Culinars\target\scala-2.11\classes...
[error] C:\Users\senok\IdeaProjects\Culinars\conf\routes:13: Cannot use a method returning java.util.concurrent.CompletionStage[play.mvc.Result] as a Handler for requests
[error] GET /search controllers.Application.search
[error] C:\Users\senok\IdeaProjects\Culinars\conf\routes:13: not enough arguments for method createInvoker: (implicit hif: play.core.routing.HandlerInvokerFactory[java.util.concurrent.CompletionStage[play.mvc.Result]])play.core.routing.HandlerInvoker[java.util.concurrent.CompletionStage[play.mvc.Result]].
[error] Unspecified value parameter hif.
[error] GET /search controllers.Application.search
[error] two errors found
[error] (compile:compile) Compilation failed
[error] application -

! @715l00c2i - Internal server error, for (GET) [/search] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[Cannot use a method returning java.util.concurrent.CompletionStage[play.mvc.Result] as a Handler for requests]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
at scala.Option.map(Option.scala:145) ~[scala-library-2.11.7.jar:na]
at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49) ~[na:na]
at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44) ~[na:na]
at scala.Option.map(Option.scala:145) ~[scala-library-2.11.7.jar:na]
at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44) ~[na:na]
at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40) ~[na:na]
at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]

编辑:这是 doSearch:

private static CompletableFuture<List<SearchResult>> doSearch(Map<String, String>[] args) {
CompletableFuture<List<SearchResult>> result = new CompletableFuture<>();
List<SearchResult> results = new ArrayList<>();
final int[] paramsLeft = new int[]{args.length};
for (Map<String, String> param : args) {
if (!param.containsKey("type")
|| !param.containsKey("uid")
|| !param.containsKey("importance")) {
paramsLeft[0]--;
if (paramsLeft[0] == 0)
result.complete(results);
} else {
String nodeName = "";
switch (param.get("type")) {
case "ingredient":
nodeName = "ingredients";
break;
case "tag":
nodeName = "tags";
break;
case "keyword":
nodeName = "keywords";
break;
default:
paramsLeft[0]--;
if (paramsLeft[0] == 0)
result.complete(results);
}
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(nodeName).child(param.get("uid"));
ref.child("recipes").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
paramsLeft[0]--;
if (paramsLeft[0] == 0)
result.complete(results);
return;
}
for (DataSnapshot recipe : dataSnapshot.getChildren()) {
SearchResult searchResult = new SearchResult();
searchResult.uid = recipe.getKey();
searchResult.matching = Double.parseDouble(param.get("importance"));
int index = results.indexOf(searchResult);
if (index > -1)
results.get(index).combine(searchResult);
else
results.add(searchResult);
}
paramsLeft[0]--;
if (paramsLeft[0] == 0) {
result.complete(results);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
paramsLeft[0]--;
if (paramsLeft[0] == 0)
result.complete(results);
}
});
}
}
return result;
}

最佳答案

只需将方法的最后一部分更改为:

SearchRequestBody search = Json.fromJson(json, SearchRequestBody.class);
return doSearch(search.body).thenApply(searchResults -> {
return ok(Json.toJson(searchResults));
});

如果您仍然遇到编译错误,请发布 doSearch

的代码

关于java - 编译错误 : Cannot us a method returning CompetionStage as a Handler for requests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041333/

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