I've been triyng to use the Jackson2JsonRedisSerializer
to serialize a custom instance as the hash value in a Redis store. It seems that even though I have correctly created the template, no hash is created.
Just a note that I'm using spring-data-reactive-redis
with spring-webflux
with Kotlin.
data class Movie(
@get:JsonProperty("Id")
val id: String?,
@get:JsonProperty("Title")
val title: String,
)
@Configuration
class RedisConfig {
@Bean
fun hashTemplate(factory: ReactiveRedisConnectionFactory): ReactiveRedisTemplate<String, Movie> {
val serializationContext = RedisSerializationContext.newSerializationContext<String, Movie>(StringRedisSerializer())
.hashKey(StringRedisSerializer())
.hashValue(Jackson2JsonRedisSerializer(Movie::class.java))
.build()
return ReactiveRedisTemplate(factory, context)
}
}
Here is an example of adding data using this template.
@Component
class DataLoader(@Qualifier("hashTemplate") private val template: ReactiveRedisTemplate<String, Movie>) {
@PostConstruct
fun loadData() {
Flux.fromIterable(
listOf(Movie("1", "Avengers: Endgame"), Movie("2", "Black Widow"))
)
.flatMap { movie ->
template.opsForHash<String, Movie>().put("Movies", movie.id, movie)
}
.thenMany(template.opsForHash<String, Movie>().entries("Movies"))
.subscribe { m -> println(m) }
}
}
Can anyone help me with why Spring is not using the template I have created.