在整合redis的时候使用注解配置缓存策略的时候出现下述错误。
java.lang.ClassCastException: com.guide.pojo.TUser cannot be cast to java.lang.String
原因是返回值为对象,而不是默认的字符串类型。强转出现错误,抛出异常。
解决办法:
配置文件中在RedisTemplate中注入序列化等属性。
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
</bean>
然后又出现
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "area_name" (class com.guide.pojo.TUser), not marked as ignorable (6 known properties: , "uname", "hname", "hunitID", "areaName", "areaId", "password"])
在pojo类上添加注解:
@JsonSerialize
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public class TUser implements Serializable {
...
OK