quarkus reactive getting started PUT and DELETE operations

2.2k Views Asked by At

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.

2

There are 2 best solutions below

2
On

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

4
On

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.