Skip to the content.

D10048 - Utility X, Pager Processing

In zero system, it provide normalized object to store pager information, it’s name is io.vertx.up.atom.query.Pager, this object could help you to do pagination. this object contains following apis for extract different metadata:

The pagination parameters may came from http request query string, body or other place, in this kind of situation, the sender could take this place to do parameter normalization.

In “Interface Style”, the sender code is ignored, but in current chapter we need this sender class to do useful things.

1. Source Code

The code segment came from real projects instead of demos

package com.tlk.micro.comment;

import io.vertx.core.json.JsonObject;
import io.vertx.up.annotations.Address;
import io.vertx.up.annotations.EndPoint;

import javax.validation.constraints.Min;

public interface IrCommentApi {

    JsonObject list(
            @QueryParam("videoId") final String videoId,
            @QueryParam("page") @Min(value = 1, message = InValid.PAGE)
            @DefaultValue("1") final Integer page,
            @QueryParam("size") @Min(value = 1, message = InValid.SIZE)
            @DefaultValue("10") final Integer size

Here we defined an interface to be annotated as EndPoint, here the parameters page and size came from query string and contains the default values. Then let’s see how to use Utility X to process the pager parameters:

package com.tlk.micro.comment;

import io.vertx.core.json.JsonObject;
import io.vertx.up.unity.Ux;
import io.vertx.up.fn.Fn;

public class IrCommentActor implements IrCommentApi {

    public JsonObject list(final String videoId,
                           final Integer page,
                           final Integer size) {
        final JsonObject params = new JsonObject();
        Fn.safeNull(() -> params.put("videoId", videoId), videoId);
        params.put("pager", Ux.toPagerJson(page, size));
        return params;

In above examples we used the method toPagerJson, this method return value is not pager, but JsonObject instead, in this method, the Actor ( Sender ) has done following things:

# request url -> xxxxx?videoId=a&page=1&size=20
# The converted data should be as following:

Then the consumer could consume normalized Json data before it called service interfaces.

In your worker you can do as following:

package com.tlk.micro.comment;

import com.tlk.eon.TargetType;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.up.unity.Ux;
import io.vertx.up.annotations.Address;
import io.vertx.up.annotations.Queue;
import io.vertx.up.commune.Envelop;

import javax.inject.Inject;

public class IrCommentWorker {

    private transient CommentStub stub;

    public void list(final Message<Envelop> message) {
        final JsonObject params = Ux.getBody(message);
        final String targetId = params.getString("videoId");
        final JsonObject filter = new JsonObject()
                .put("targetId", targetId)
                .put("targetType", TargetType.VIDEO);
        this.stub.query(filter, MongoReadOpts.toFull(
                Ux.toSorter("commentTime", false))

You can ignored the code logical details of this example, but here you could see we called: toPager(JsonObject) api to convert the params’s pager node to valid Pager object.

2. Pager Apis

Actually zero system provide three standard pager related apis:

3. Summary

The pager object of io.vertx.up.atom.query.Pager is defined by zero system and provide small interfaces to developer to do normalized Pager building, in this situation you can finish any kind of pagination in zero system instead of implement for each projects.