Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReactiveStream/JDK 9 Flow Support #1281

Open
hantsy opened this issue Aug 30, 2024 · 3 comments
Open

Add ReactiveStream/JDK 9 Flow Support #1281

hantsy opened this issue Aug 30, 2024 · 3 comments

Comments

@hantsy
Copy link

hantsy commented Aug 30, 2024

Besides asynchronous context support in Rest, add ReactiveStreams Publisher and JDK 9 Flow.Publisher as a generic return type, or parameter type in Response.

Quarkus/Resteasy has implemented reactive versions.

The following is an example of Quarkus/Resteasy Reactive, check: https://github.com/hantsy/quarkus-sandbox/blob/master/resteasy-reactive-hibernate/src/main/java/com/example/PostResource.java

@Path("/posts")
@RequestScoped
public class PostResource {
    private final static Logger LOGGER = Logger.getLogger(PostResource.class.getName());

    private final PostRepository posts;

    @Inject
    public PostResource(PostRepository posts) {
        this.posts = posts;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<List<Post>> getAllPosts() {
        return this.posts.findAll();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Uni<Response> savePost(@Valid Post post) {
        return this.posts.save(post)
                .map(id -> created(URI.create("/posts/" + id)).build());
    }

    @Path("{id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<Response> getPostById(@PathParam("id") final String id) {
        return this.posts.findById(UUID.fromString(id))
                .map(data -> {
                    if (data == null) {
                        return null;
                    }
                    return ok(data).build();
                })
        //        .onItem().ifNull().continueWith(status(Status.NOT_FOUND).build());
        .onFailure(PostNotFoundException.class).recoverWithItem(status(Status.NOT_FOUND).build());
    }

    @Path("{id}")
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public Uni<Response> updatePost(@PathParam("id") final String id, @Valid Post post) {
        return this.posts.update(UUID.fromString(id), post)
                .map(updated -> updated > 0 ? Status.NO_CONTENT : Status.NOT_FOUND)
                .map(status -> status(status).build());
    }

    @DELETE
    @Path("{id}")
    public Uni<Response> delete(@PathParam("id") String id) {
        return this.posts.delete(UUID.fromString(id))
                .map(deleted -> deleted > 0 ? Status.NO_CONTENT : Status.NOT_FOUND)
                .map(status -> status(status).build());
    }
}

Besides general Reactive Streams support, the Server Sent Event is a good match for Reactive Streams.

  • Support Publisher<ServerSentEvent>
  • support Publisher<POJO> and content type text/event-stream to transform and write the parameterized data into the data in the SSE stream.

And MP Rest Client has already supported ReactiveStreams for a while.

@mkarg
Copy link
Contributor

mkarg commented Aug 30, 2024

In fact I was about to propose that very same feature, too! 👍

@jamezp
Copy link
Contributor

jamezp commented Sep 12, 2024

I'm a +1 for this. I think it makes sense.

@mkarg
Copy link
Contributor

mkarg commented Dec 17, 2024

Anybody in the mood for authoring a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants