|
|
@@ -1,74 +0,0 @@
|
|
|
-package com.weblux.launchredis.config;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
-import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.weblux.launchredis.test.User;
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
|
|
|
-import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
|
|
-import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
-import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
-import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
-import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
-
|
|
|
-/**
|
|
|
- * 反应redis配置
|
|
|
- */
|
|
|
-@Configuration
|
|
|
-public class ReactiveRedisConfig {
|
|
|
-
|
|
|
- @Bean
|
|
|
- public RedisSerializationContext<String, Object> redisSerializationContext() {
|
|
|
- RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder =
|
|
|
- RedisSerializationContext.newSerializationContext();
|
|
|
- Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
|
- ObjectMapper om = new ObjectMapper();
|
|
|
- // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
|
|
|
- om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
- // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
|
|
|
- om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
- redisSerializer.setObjectMapper(om);
|
|
|
-
|
|
|
- builder.key(new StringRedisSerializer());
|
|
|
- builder.value(redisSerializer);
|
|
|
- builder.hashKey(new StringRedisSerializer());
|
|
|
- builder.hashValue(redisSerializer);
|
|
|
-
|
|
|
- return builder.build();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and a typed
|
|
|
- * {@link Jackson2JsonRedisSerializer}.
|
|
|
- */
|
|
|
- @Bean
|
|
|
- public ReactiveRedisTemplate<String, User> reactiveJsonUserRedisTemplate(
|
|
|
- ReactiveRedisConnectionFactory connectionFactory) {
|
|
|
-
|
|
|
- var serializer = new Jackson2JsonRedisSerializer<User>(User.class);
|
|
|
- RedisSerializationContext.RedisSerializationContextBuilder<String, User> builder = RedisSerializationContext
|
|
|
- .newSerializationContext(new StringRedisSerializer());
|
|
|
-
|
|
|
- var serializationContext = builder.value(serializer).build();
|
|
|
-
|
|
|
- return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
|
|
|
- }
|
|
|
- @Bean
|
|
|
- public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
|
|
|
- RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
|
|
|
- .<String, Object>newSerializationContext(new StringRedisSerializer())
|
|
|
- .hashKey(new StringRedisSerializer())
|
|
|
- .hashValue(new Jackson2JsonRedisSerializer<>(Object.class))
|
|
|
- .build();
|
|
|
-
|
|
|
- return new ReactiveRedisTemplate<>(factory, serializationContext);
|
|
|
- }
|
|
|
-
|
|
|
-// @Bean
|
|
|
-// public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
|
|
|
-// return new ReactiveRedisTemplate<>(connectionFactory,redisSerializationContext());
|
|
|
-// }
|
|
|
-}
|
|
|
-
|