无法在 Spring Reactive Redis 缓存中存储布尔值
Not able to Store Boolean in Spring Reactive Redis Cache
" java.lang.IllegalStateException: Cannot serialize value of type class java.lang.Boolean without a serializer
at org.springframework.data.redis.serializer.DefaultRedisElementWriter.write(DefaultRedisElementWriter.java:54) ~[spring-data-redis-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.write(RedisSerializationContext.java:287) ~[spring-data-redis-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
at org.springframework.data.redis.core.DefaultReactiveHashOperations.rawHashValue(DefaultReactiveHashOperations.java:286) ~[spring-data-redis-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
我在存储布尔值时遇到此异常。
我的redis配置:
ReactiveRedisTemplate<String, Object> reactiveRedisTemplateGeneric(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder =
RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
RedisSerializationContext<String, Object> context = builder.value(valueSerializer).build();
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, context);
}
我们还需要在构建上下文时在 hashValue 中设置序列化器..
RedisSerializationContext<String, Object> context = builder.value(valueSerializer).hashValue(valueSerializer).build();
" java.lang.IllegalStateException: Cannot serialize value of type class java.lang.Boolean without a serializer
at org.springframework.data.redis.serializer.DefaultRedisElementWriter.write(DefaultRedisElementWriter.java:54) ~[spring-data-redis-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.write(RedisSerializationContext.java:287) ~[spring-data-redis-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
at org.springframework.data.redis.core.DefaultReactiveHashOperations.rawHashValue(DefaultReactiveHashOperations.java:286) ~[spring-data-redis-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
我在存储布尔值时遇到此异常。
我的redis配置:
ReactiveRedisTemplate<String, Object> reactiveRedisTemplateGeneric(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder =
RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
RedisSerializationContext<String, Object> context = builder.value(valueSerializer).build();
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, context);
}
我们还需要在构建上下文时在 hashValue 中设置序列化器..
RedisSerializationContext<String, Object> context = builder.value(valueSerializer).hashValue(valueSerializer).build();