The spring-boot integration redis startup project reports an error with the following message:
defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.
orConsider defining a bean of type 'org.springframework.data.redis.core.StringRedisTemplate' in your configuration.
Modify the pom file
Change the
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
is changed to
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
After modification, if you want to customize redistemplate, you can refer to the following configuration
@Bean
public RedisTemplate<String, Object> template(RedisConnectionFactory factory) {
// Create RedisTemplate<String, Object>object
RedisTemplate<String, Object> template = new RedisTemplate<>();
// Configuring the connection factory
template.setConnectionFactory(factory);
// Define the Jackson2JsonRedisSerializer serialization object
Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
// Specify the field, field, get and set to be serialized, and the range of modifiers, ANY of which are included in private and public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// Specify the type of serialized input, the class must be non-final modified, final modified class, such as String, Integer, etc. will report an exception
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);
jacksonSeial.setObjectMapper(om);
StringRedisSerializer stringSerial = new StringRedisSerializer();
// redis key Serialization method used stringSerial
template.setKeySerializer(stringSerial);
// redis value Serialization method usedjackson
template.setValueSerializer(jacksonSeial);
// redis hash key Serialization method usedstringSerial
template.setHashKeySerializer(stringSerial);
// redis hash value Serialization method usedjackson
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}