Quarkus反应性入门和删除操作

发布于 2025-01-25 02:15:56 字数 2244 浏览 7 评论 0 原文

我正在浏览 quarkus Quarkus反应性入门页面和put and delete方法实现是丢失的。

似乎假设我们已经知道Quarkus,并且只是在阅读指南以从反应性转变为反应性。为什么他们不提供完整的例子,我不知道。我的意思是,如果不是指导,或者有人向您展示它是如何完成的?

放置应更换条目,并删除应删除。

PUT /{id} = replace
DELETE /{id} = delete

我的实体不是水果,而不是果实。

package de.icod.reso.resources;

import de.icod.reso.entities.Profile;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.quarkus.panache.common.Sort;
import io.smallrye.mutiny.Uni;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;
import java.util.UUID;

@Path("/profile")
@ApplicationScoped
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ProfileResource {
    @GET
    public Uni<List<Profile>> get() {
        return Profile.listAll(Sort.by("name"));
    }

    @GET
    @Path("/{id}")
    public Uni<Profile> getSingle(UUID id) {
        return Profile.findById(id);
    }

    @POST
    public Uni<Response> create(Profile profile) {
        return Panache.<Profile>withTransaction(profile::persist)
                .onItem().transform(inserted -> Response.created(URI.create("/profile" + inserted.id)).build());
    }

    @PUT
    @Path("/{id}")
    public Uni<Response> replace(UUID id, Profile profile) {
        // error: incompatible types: incompatible parameter types in method reference       
        return Panache.<Profile>withTransaction(profile::update)
                .onItem().transform(updated -> Response.ok(URI.create("/profile" + updated.id)).build());
    }
    
    @DELETE
    @Path("/{id}")
    public Uni<Response> delete(UUID id) {
        // delete entity by id
    }
}

您可以填写两个丢失的功能吗?

我知道有

但内容与入门页面中写的内容不同。

I'm going through the quarkus reactive getting started page and PUT and DELETE method implementations are missing.

Seem like it assumes we already know quarkus and are just reading the guide to switch from non-reactive to reactive. Why they don't provide a full example, I don't know. I mean where would you learn if not by a guide or someone showing you how it's done?

PUT should replace an entry and DELETE should delete one.

PUT /{id} = replace
DELETE /{id} = delete

Instead of Fruit my entity is named Profile.

package de.icod.reso.resources;

import de.icod.reso.entities.Profile;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.quarkus.panache.common.Sort;
import io.smallrye.mutiny.Uni;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;
import java.util.UUID;

@Path("/profile")
@ApplicationScoped
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ProfileResource {
    @GET
    public Uni<List<Profile>> get() {
        return Profile.listAll(Sort.by("name"));
    }

    @GET
    @Path("/{id}")
    public Uni<Profile> getSingle(UUID id) {
        return Profile.findById(id);
    }

    @POST
    public Uni<Response> create(Profile profile) {
        return Panache.<Profile>withTransaction(profile::persist)
                .onItem().transform(inserted -> Response.created(URI.create("/profile" + inserted.id)).build());
    }

    @PUT
    @Path("/{id}")
    public Uni<Response> replace(UUID id, Profile profile) {
        // error: incompatible types: incompatible parameter types in method reference       
        return Panache.<Profile>withTransaction(profile::update)
                .onItem().transform(updated -> Response.ok(URI.create("/profile" + updated.id)).build());
    }
    
    @DELETE
    @Path("/{id}")
    public Uni<Response> delete(UUID id) {
        // delete entity by id
    }
}

Can you fill the 2 missing functions?

I'm aware there is Quarkus Getting Started (w/ Reactive): PostGres/Docker CRUD Application -- Missing POST, PUT, and DELETE REST URLs

but the contents are different from what's written in the getting started page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2025-02-01 02:15:56

一般而言,入门和Quick Starts可能会略有不同。原因是入门的目的是帮助新用户快速设置一个小型工作项目,而QuickStart是展示所涉及的扩展功能的不同功能的地方。

我们试图确保文档始终是最新的,但是没有什么比快速启动之类的工作示例了。

在这种情况下,我不明白您的抱怨。

An example of PUT and DELETE is available in the

    @PUT
    @Path("{id}")
    public Uni<Response> update(Long id, Fruit fruit) {
        return Panache
                .withTransaction(() -> Fruit.<Fruit> findById(id)
                    .onItem().ifNotNull().invoke(entity -> entity.name = fruit.name)
                )
                .onItem().ifNotNull().transform(entity -> Response.ok(entity).build())
                .onItem().ifNull().continueWith(Response.ok().status(NOT_FOUND)::build);
    }

    @DELETE
    @Path("{id}")
    public Uni<Response> delete(Long id) {
        return Panache.withTransaction(() -> Fruit.deleteById(id))
                .map(deleted -> deleted
                        ? Response.ok().status(NO_CONTENT).build()
                        : Response.ok().status(NOT_FOUND).build());
    }

对于您的用例> fruit 和 uuid 而不是 long ID。我认为您不需要其他任何东西才能使它起作用。

//错误:不兼容类型:方法参考中的不兼容参数类型

错误消息告诉您问题的原因:语法 panache.withtransaction(profile :: Update :: Update)不正确。

这是不起作用的,因为 profile.update(...)期望在使用 withtransaction 的方法参考时,您不会传递其他参数。
另一方面,可以使用 panache.withtransaction(profile :: persist)因为 profile.persist()是一种不需要参数的方法。

这就是方法参考在Java中的工作方式。

也就是说,文档从未完成,并且根据我们收到的反馈会随着时间的流逝而变得更好。

另外,Stackoverflow可能不是解决此类问题的最佳场所。
我可能会在 用户首先。如果您推出一个在某处给您麻烦的代码的示例,那么收到答案的速度更快,这样我们就可以运行它并为您提供有关它的问题的精确帮助。

我知道有Quarkus入门(W/反应性):Postgres/Docker Crud应用程序 - 缺少帖子,put和删除REST URL

此问题中的示例为2年,此后发生了变化。实际上,该问题中引用的同一Quickstart和Tutorial现在与我使用的代码匹配。

Generally speaking, the Getting Started and the quickstarts might be slightly different. The reason is that the purpose of the Getting Started is to help a new user to set up a small working project quickly and the quickstart is a place to showcase different functionalities of the extensions involved.

We try to make sure that the documentation is always up to date but nothing beats a working example like the quickstart.

In this case I don't understand your complains.

An example of PUT and DELETE is available in the Hibernate Reactive with panache quickstart:

    @PUT
    @Path("{id}")
    public Uni<Response> update(Long id, Fruit fruit) {
        return Panache
                .withTransaction(() -> Fruit.<Fruit> findById(id)
                    .onItem().ifNotNull().invoke(entity -> entity.name = fruit.name)
                )
                .onItem().ifNotNull().transform(entity -> Response.ok(entity).build())
                .onItem().ifNull().continueWith(Response.ok().status(NOT_FOUND)::build);
    }

    @DELETE
    @Path("{id}")
    public Uni<Response> delete(Long id) {
        return Panache.withTransaction(() -> Fruit.deleteById(id))
                .map(deleted -> deleted
                        ? Response.ok().status(NO_CONTENT).build()
                        : Response.ok().status(NOT_FOUND).build());
    }

For your use case you need to use Profile instead of Fruit, and UUID instead of Long for the id. I don't think you need anything else to make it work.

// error: incompatible types: incompatible parameter types in method reference

The error message is telling you the cause of the problem: the syntax Panache.withTransaction(profile::update) is not correct.

This won't work because profile.update(...) expects additional parameters that you are not passing when using method reference with withTransaction.
On the other hand, one can use Panache.withTransaction(profile::persist) because profile.persist() is a method that doesn't require parameters.

This is how method reference works in Java.

That said, the documentation is never done and it will get better over time based on the feedback we receive.

Also, StackOverflow might not be the best place for this type of questions.
I would have probably asked this on the users stream on Zulip first. It's even faster to receive an answer if you push an example of the code that's giving you trouble somewhere, so that we can run it and give you precise help about what's wrong with it.

I'm aware there is Quarkus Getting Started (w/ Reactive): PostGres/Docker CRUD Application -- Missing POST, PUT, and DELETE REST URLs

The example in this question is 2 years old and things have changed since then. In fact, the same quickstart and tutorial referenced in that question now match the code I've used.

绝影如岚 2025-02-01 02:15:56

You can find the information you are asking for at https://quarkus.io/guides/hibernate-orm-panache#writing-a-jax-rs-resource.

You can also compare reactive vs. non-reactive versions of the same application by looking at the Hibernate Reactive Panache Quickstart and the Hibernate ORM Panache Quickstart

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文