Uni<Void> 如何在 REST 调用中获得失败或成功响应

Uni<Void> how to get failure or success response at REST call

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

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

刚给出一个响应接受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 到 return 直接 Uni 并避免转换为 CompletionStage,但如果您是定期这样做。