Depending on a flag in the request body, how do i return bulk JSON or SSE on the same route in a quarkus app?

  Kiến thức lập trình

Let’s say I have a quarkus resource like the following:

@ApplicationScoped
@Path("/some")
public class SomeResource {
    @Inject
    SomeService service;

    @POST
    @Path("/path")
    @Produces(MediaType.SERVER_SENT_EVENTS)
    @Consumes(MediaType.APPLICATION_JSON)
    @RestStreamElementType(MediaType.APPLICATION_JSON)
    public Multi<SomeResponse> streamResponses(@Body SomeRequest someRequest) {
        return service.stream(someRequest);
    }
}

and the SomeRequest class can contain a flag called stream, which indicates if the response should be streamed or returned as bulk json request. The service already has the capability to return bulk json. How can i return bulk responses or stream responses on the same route depending on the stream flag?

The resource with the additional endpoint should look something like this:

@ApplicationScoped
@Path("/some")
public class SomeResource {
    @Inject
    SomeService service;

    @POST
    @Path("/path")
    @Produces(MediaType.SERVER_SENT_EVENTS)
    @Consumes(MediaType.APPLICATION_JSON)
    @RestStreamElementType(MediaType.APPLICATION_JSON)
    public Multi<SomeResponse> streamResponses(@Body SomeRequest someRequest) {
        return service.stream(someRequest);
    }
    
    @POST
    @Path("/path")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public SomeResponse streamResponses(@Body SomeRequest someRequest) {
        return service.chat(someRequest);
    }

}

I have tried to use Reactive Routes, but could not find out how to conditionally set the return type correctely. Using a Uni resulted in the wrong content-type of the response

New contributor

user26614759 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT