Search in sources :

Example 1 with GenericJackson2JsonRedisSerializer

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);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer)

Example 2 with GenericJackson2JsonRedisSerializer

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());
}
Also used : GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) AdviceMessage(org.springframework.integration.message.AdviceMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) AdviceMessage(org.springframework.integration.message.AdviceMessage) Date(java.util.Date) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 3 with GenericJackson2JsonRedisSerializer

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;
}
Also used : GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) GenericToStringSerializer(org.springframework.data.redis.serializer.GenericToStringSerializer) Bean(org.springframework.context.annotation.Bean)

Aggregations

GenericJackson2JsonRedisSerializer (org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 Bean (org.springframework.context.annotation.Bean)1 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)1 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)1 GenericToStringSerializer (org.springframework.data.redis.serializer.GenericToStringSerializer)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1 AdviceMessage (org.springframework.integration.message.AdviceMessage)1 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)1 MessageGroup (org.springframework.integration.store.MessageGroup)1 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)1 MutableMessage (org.springframework.integration.support.MutableMessage)1 Message (org.springframework.messaging.Message)1 ErrorMessage (org.springframework.messaging.support.ErrorMessage)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1