'CosmosDB pagination with requestContinuation not returning results

I'm using JPA with CosmosDB to fetch entries from containers. I have to implement pagination and, according to the documentation, it should rely on the requestContinuation string, which is null on the first request and not null afterwards.

To make things more simple, I created a basic app which calls the repository, takes the requestContinuation object and then makes a new request using it's value.

I have 8 objects in the CosmosDB container and the page size is 5. The first request returns 5 elements and the requestionContinuation value

{"compositeToken":"{\"token\":null,\"range\":{\"min\":\"\",\"max\":\"FF\"}}","orderByItems":[{"propertyBag":{"item":[2021,5,20,14,13,52,716826900]},"item":[2021,5,20,14,13,52,716826900],"map":{"item":[2021,5,20,14,13,52,716826900]}}],"rid":"7eYNAKj8mAMIAAAAAAAAAA==","inclusive":true}

I use it, AS IT IS, to make a new request

    Pageable pageable = new CosmosPageRequest(0, size, requestContinuation, Sort.by(sort));
    return repo.findAllByUserId(userId, pageable);

The result is empty and the new requestContinuation is null. The page object correctly states that the total number is 8 but never returns the missing 3. The value of the requestContinuation is not a valid JSON, if you analyze it. I also tried replacing the \ from the compositeToken part of the JSON, which made it a valid JSON. If the invalid JSON is accepted by CosmosDB but the result is empty, the valid JSON is rejected as being INVALID :)

I looked into various sources and I saw something about encoding. I tried to URLEncoder class but it didn't work.

Does anybody have an idea?

Thanks.

UPDATE The query is also sorting by the creation date. If sorting is removed, the new token works. It's structure is different.

Old token - with sorting:

{"compositeToken":"{\"token\":null,\"range\":{\"min\":\"\",\"max\":\"FF\"}}","orderByItems":[{"propertyBag":{"item":[2021,5,20,14,13,52,716826900]},"item":[2021,5,20,14,13,52,716826900],"map":{"item":[2021,5,20,14,13,52,716826900]}}],"rid":"7eYNAKj8mAMIAAAAAAAAAA==","inclusive":true}

New token - no sorting:

{"token":"+RID:~7eYNAKj8mAMKAAAAAAAAAA==#RT:1#TRC:5#ISV:2#IEO:65567#QCF:4#FPC:AQoAAAAAAAAADQAAAAAAAAA=","range":{"min":"","max":"FF"}}


Solution 1:[1]

The second request, with the continuationToken, was returning an empty response because the ordering field was a LocalDateTime field, which in CosmosDB is serialized as an array. I fixed the issue by adding this to the LocalDateTime field:

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss'Z'")

This way, the sorting field is a string and can be used for ordering.

Solution 2:[2]

Just Encode the Request Token and it will work like charm

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cristian
Solution 2 Rehan