如何将Uni<Void>响应转换为Quarkus Resteasy Reactive中的“未找到”?

huangapple go评论67阅读模式
英文:

How to convert a Uni<Void> response to a "not found" in Quarkus Resteasy Reactive?

问题

someService.fetchStuff(id)返回Uni&lt;Void&gt;时,端点将返回状态码204(未找到)的空HTTP响应。在这种情况下,如何使端点发送状态码404(未找到)的HTTP响应?

英文:

Given an endpoint like that:

@GET
@Path(&quot;{id}&quot;)
public Uni&lt;Stuff&gt; getSomeStuff(@PathParam(&quot;id&quot;) int id) {
   return someService.fetchStuff(id);
}

When someService.fetchStuff(id) returns an Uni&lt;Void&gt; the endpoints returns an empty HTTP response with status 204 (not found).

How can I get the endpoint to send an HTTP response with status 404 (not found) in this case?

答案1

得分: 1

你可以像这样做:

@GET
@Path("{id}")
public Uni<RestResponse<Stuff>> getSomeStuff(@PathParam("id") int id) {
   return someService.fetchStuff(id).onItem().transform(s -> {
            if (test(s)) {
                return RestResponse.ok(s);
            } else {
                return RestResponse.notFound();
            }
        })
}
英文:

You can do something like this:

@GET
@Path(&quot;{id}&quot;)
public Uni&lt;RestResponse&lt;Stuff&gt;&gt; getSomeStuff(@PathParam(&quot;id&quot;) int id) {
   return someService.fetchStuff(id).onItem().transform(s -&gt; {
            if (test(s)) {
                return RestResponse.ok(s);
            } else {
                return RestResponse.notFound();
            }
        })
}

huangapple
  • 本文由 发表于 2023年7月24日 17:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定