Mustaq
Mustaq

Reputation: 45

Using io.smallrye.mutiny.Uni to create a response object

I am trying to learn using ReactiveMongoClient on a Quarkus framework.

I am partly successful with sending a response as Uni>

@GET
@Path("/unpaginated")
public Uni<List<Staff>> unpaginatedStaffList() {
    return staffService.getStaffResponse();
}

But when I try to get an object of some other class (StaffResponse) to include a Link object for pagination, I am not getting any Staff record. (For now I have hardcoded Link for pagination )

@GET
@Path("/paginated")
public StaffResponse paginatedStaffList() {
    List<Link> links = LinkService.getLinks("/staff?page=2&limit=20", "next");
    Uni<List<Staff>> staff = (Uni<List<Staff>>) staffService.getStaffResponse();
    return new StaffResponse(links, staff);
}

"staff" is empty in the response.

MongoClient is returning list of Staff ok, looks like the Response object is not getting the list. Tried reading SmallRye Mutiny documentations - could not work out.

Please help.

I have committed the code at: https://github.com/doepradhan/staffApi and a sample json data file (https://github.com/doepradhan/staffApi/blob/master/sample-staff-data.json)

Thank you heaps for you kind help.

Upvotes: 4

Views: 8164

Answers (1)

Dmytro Chaban
Dmytro Chaban

Reputation: 1213

You can't mix up two approaches. You need to use Uni as an output for the endpoint. That means that you need to convert both input sources into Uni, combine them, and map into StaffResponse.

  1. Convert LinkService to return Uni(or use Uni.createFrom().item(links))
  2. Change StaffResoponse to only use simple objects:
public StaffResponse(List<Link> links, List<Staff> staff) {
        this.links = links;
        this.staff = staff;
    }
  1. As said before, get two Uni sources and combine them together, you'll get Uni<Tuple>, map it then to StaffResponse:
    @GET
    @Path("/paginated")
    public Uni<StaffResponse> paginatedStaffList() {
        final Uni<List<Link>> links =
                Uni.createFrom().item(LinkService.getLinks("/staff?page=2&limit=20", "next"));
        Uni<List<Staff>> staff = staffService.getStaffResponse();
        return staff.and(links).map(it -> new StaffResponse(it.getItem2(), it.getItem1()));
    }

I created a working PR here

Upvotes: 5

Related Questions