use of org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer in project pancm_project by xuwujing.
the class RedisConfig method initDomainRedisTemplate.
/**
* 设置数据存入 redis 的序列化方式,并开启事务
* @param redisTemplate
* @param factory
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
// 如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 开启事务
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(factory);
}
use of org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer in project spring-integration by spring-projects.
the class RedisMessageGroupStoreTests method testJsonSerialization.
@Test
@RedisAvailable
public void testJsonSerialization() {
RedisConnectionFactory jcf = getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
Message<?> genericMessage = new GenericMessage<>(new Date());
Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
store.addMessagesToGroup(this.groupId, genericMessage, mutableMessage, adviceMessage, errorMessage);
MessageGroup messageGroup = store.getMessageGroup(this.groupId);
assertEquals(4, messageGroup.size());
List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
assertEquals(genericMessage, messages.get(0));
assertEquals(mutableMessage, messages.get(1));
assertEquals(adviceMessage, messages.get(2));
Message<?> errorMessageResult = messages.get(3);
assertEquals(errorMessage.getHeaders(), errorMessageResult.getHeaders());
assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
assertEquals(errorMessage.getOriginalMessage(), ((ErrorMessage) errorMessageResult).getOriginalMessage());
assertEquals(errorMessage.getPayload().getMessage(), ((ErrorMessage) errorMessageResult).getPayload().getMessage());
Message<Foo> fooMessage = new GenericMessage<>(new Foo("foo"));
try {
store.addMessageToGroup(this.groupId, fooMessage).getMessages().iterator().next();
fail("SerializationException expected");
} catch (Exception e) {
assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
assertThat(e.getMessage(), containsString("The class with " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo and name of " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo " + "is not in the trusted packages:"));
}
mapper = JacksonJsonUtils.messagingAwareMapper(getClass().getPackage().getName());
serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
store.removeMessageGroup(this.groupId);
messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
assertEquals(1, messageGroup.size());
assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
mapper = JacksonJsonUtils.messagingAwareMapper("*");
serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
store.removeMessageGroup(this.groupId);
messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
assertEquals(1, messageGroup.size());
assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
}
use of org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer in project moon by gentoo111.
the class RedisConfig method redisTemplate.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean("redisTemplate")
public RedisTemplate redisTemplate(@Lazy RedisConnectionFactory connectionFactory) {
RedisTemplate redis = new RedisTemplate();
GenericToStringSerializer<String> keySerializer = new GenericToStringSerializer<String>(String.class);
redis.setKeySerializer(keySerializer);
redis.setHashKeySerializer(keySerializer);
GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
redis.setValueSerializer(valueSerializer);
redis.setHashValueSerializer(valueSerializer);
redis.setConnectionFactory(connectionFactory);
return redis;
}
Aggregations