Passing LocalDate to Between comparison for Cosmos DB

3 weeks ago 14
ARTICLE AD BOX

I have Cosmos DB database with the following sample data in the container:

{ "postedDate": "2025-01-01", ... Other fields }

Repository:

public interface TransactionRepo extends ReactiveCosmosRepository<Transaction, String> { Flux<Transaction> findByPostedDateBetween(LocalDate startPostedDate, LocalDate endPostedDate); }

Entity:

@Container("containerName") public class Transaction{ private LocalDate postedDate; // Other fields including partition key, id, etc }

When I call the findByPostedDateBetween() method, no data is returned. It turned out that the problem is that the internal Azure SDK ObjectMapper created in com.azure.cosmos.implementation.Utils.createAndInitializeObjectMapper() serializes startPostedDate and endPostedDate into an array [ year, month, date ], which certainly kills the comparison. There is no way known to me to update the object mapper configuration to add write-dates-as-timestamps=false.

I don't want to keep dates as strings, but so far I don't see any other way. Perplexity suggested several options including a custom converter, but none of them worked.

Spring Boot 3, Java 17.

Read Entire Article