diff --git a/data/data-redis/src/appTest/java/com/example/data/redis/DataRedisApplicationAotTests.java b/data/data-redis/src/appTest/java/com/example/data/redis/DataRedisApplicationAotTests.java index 40b62561..6348678a 100644 --- a/data/data-redis/src/appTest/java/com/example/data/redis/DataRedisApplicationAotTests.java +++ b/data/data-redis/src/appTest/java/com/example/data/redis/DataRedisApplicationAotTests.java @@ -40,6 +40,19 @@ class DataRedisApplicationAotTests { }); } + @Test + void jackson2hashMapper(AssertableOutput output) { + + Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { + assertThat(output).hasLineMatching("hash-mapper-default-raw: .*firstname=hashed-fn.*"); + assertThat(output).hasSingleLineContaining( + "hash-mapper-default-mapped: Person{firstname='hashed-fn', lastname='hashed-ln'}"); + assertThat(output).hasLineMatching("hash-mapper-flat-raw: .*firstname=hashed-fn.*"); + assertThat(output).hasSingleLineContaining( + "hash-mapper-flat-mapped: Person{firstname='hashed-fn', lastname='hashed-ln'}"); + }); + } + @Test void jsonSerializer(AssertableOutput output) { Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { diff --git a/data/data-redis/src/main/java/com/example/data/redis/CLR.java b/data/data-redis/src/main/java/com/example/data/redis/CLR.java index 41165f27..20f00359 100644 --- a/data/data-redis/src/main/java/com/example/data/redis/CLR.java +++ b/data/data-redis/src/main/java/com/example/data/redis/CLR.java @@ -1,6 +1,7 @@ package com.example.data.redis; import java.util.Arrays; +import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; @@ -8,6 +9,7 @@ import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.hash.Jackson2HashMapper; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @@ -35,6 +37,8 @@ class CLR implements CommandLineRunner { templateOperations(); keyBoundOperations(); redisBackedSet(); + hashMapper(HashStructure.DEFAULT); + hashMapper(HashStructure.FLAT); jsonSerializer(); pubSub(); @@ -67,6 +71,16 @@ class CLR implements CommandLineRunner { System.out.printf("redis set: %s%n", redisSet.iterator().next()); } + private void hashMapper(HashStructure structure) { + + Jackson2HashMapper hashMapper = new Jackson2HashMapper(HashStructure.FLAT.equals(structure)); + template.opsForHash().putAll("hash", hashMapper.toHash(new Person("hashed-fn", "hashed-ln"))); + + Map hashedEntry = template.opsForHash().entries("hash"); + System.out.printf("hash-mapper-%s-raw: %s%n", structure, hashedEntry); + System.out.printf("hash-mapper-%s-mapped: %s%n", structure, hashMapper.fromHash(hashedEntry)); + } + private void keyBoundOperations() { BoundValueOperations keyBoundOps = template.boundValueOps("bound-key"); keyBoundOps.set("OK"); @@ -107,4 +121,15 @@ class CLR implements CommandLineRunner { System.out.printf("pub/sub: %s%n", messageHandler.receivedMessages()); } + enum HashStructure { + + DEFAULT, FLAT; + + @Override + public String toString() { + return name().toLowerCase(); + } + + } + }