gpt4 book ai didi

java - Uni 如何在 REST 调用中获得失败或成功响应

转载 作者:行者123 更新时间:2023-12-01 14:31:55 28 4
gpt4 key购买 nike

在服务器发送电子邮件后,我试图设置一个简单的成功/失败响应。

但是,即使在尝试了许多变体数小时后,我仍然没有得到正确的响应。

example code那只是给出了 已接受回复 在这儿:

@GET
@Path("/async")
public CompletionStage<Response> sendASimpleEmailAsync() {
return reactiveMailer.send(
Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body"))
.subscribeAsCompletionStage()
.thenApply(x -> Response.accepted().build());
}

但是,当邮件没有成功发送时,我想在这里提供另一个回复。我试过的是这个(但这是一个没有成功的 Uni 类型转换):
@GET
@Path("/async")
public Uni<Void> sendASimpleEmailAsync() {
final Mail mailToBeSent = Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body");

return (Uni<Void>) reactiveMailer.send(mailToBeSent)
.then( response -> {
if (response == null) {
return Response.accepted();
}
});
}

控制台输出(由于密码错误而未发送邮件时):
[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:1.5.1.Final:dev (default-cli) on project h21-microservices: Unable to execute mojo: Compilation failure: 
[ERROR] /FeedbackResource.java:[36,32] lambda body is neither value nor void compatible
[ERROR] /FeedbackResource.java:[36,13] method then in interface io.smallrye.mutiny.Uni<T> cannot be applied to given types;
[ERROR] required: java.util.function.Function<io.smallrye.mutiny.Uni<java.lang.Void>,O>
[ERROR] found: (response)[...]; } }
[ERROR] reason: cannot infer type-variable(s) O
[ERROR] (argument mismatch; bad return type in lambda expression
[ERROR] missing return value)
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

还有一个选择:
@GET
@Path("/async")
public Cancellable sendASimpleEmailAsync() {
final Mail mailToBeSent = Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body");

Uni<Void> stage = reactiveMailer.send(mailToBeSent);
return stage.subscribe().with(
result -> { System.out.println("Result with " + result); Response.accepted(); },
failure -> { System.out.println("Failure with " + failure); Response.status(Status.BAD_GATEWAY); }
);
}

控制台日志(带有 println)。它在我收到接受的客户端输出后执行。
Failure with io.vertx.core.impl.NoStackTraceThrowable: AUTH CRAM-MD5 failed 530 Invalid username or password

客户端输出(由于密码错误而未发送邮件时):
HTTP/1.1 200 OK
Content-Length: 57
Content-Type: text/plain;charset=UTF-8

io.smallrye.mutiny.helpers.UniCallbackSubscriber@3fa06fdb

但是机器人没有成功。我只想接收邮件是否已发送或发送是否有任何错误。有没有人有任何关于如何进行的想法提示?

最佳答案

你可以使用 mutiny 的 onFailure().recoverWithItem()指定在失败时使用的单独响应的功能:

@GET
@Path("/async")
public Uni<Response> sendASimpleEmailAsync() {
return reactiveMailer.send(
Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body"))
.map(a -> Response.accepted().build())
.onFailure().recoverWithItem(Response.serverError().build());
}
请注意,您需要 quarkus-resteasy-mutiny返回 Uni直接并避免转换为 CompletionStage ,但如果你经常这样做,它会更有意义。

关于java - Uni<Void> 如何在 REST 调用中获得失败或成功响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62362768/

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