海淀网站建设哪家公司好,网站空间容量,商务互联做网站怎么样,凡科做网站给后台的吗背景
最近遇到了两个Redis相关的问题#xff0c;趁着清明假期#xff0c;梳理整理。
1.存入Long类型对象#xff0c;在代码中使用Long类型接收#xff0c;结果报类型转换错误。
2.String对象的反序列化问题#xff0c;直接在Redis服务器上新增一个key-value#xff0c…背景
最近遇到了两个Redis相关的问题趁着清明假期梳理整理。
1.存入Long类型对象在代码中使用Long类型接收结果报类型转换错误。
2.String对象的反序列化问题直接在Redis服务器上新增一个key-value而后在代码中get(key)时报反序列化失败。 关于Long类型转换错误 Redis的配置如下
Redis中序列化相关的配置我这里采用的是GenericJackson2JsonRedisSerializer类型的序列化方式(这种方式会有一个类型转换的坑下面会提到) Configuration AutoConfigureAfter(RedisAutoConfiguration.class) public class RedisConfiguration { Bean public RedisTemplateString, Object redisTemplate(JedisConnectionFactory redisConnectionFactory) { RedisTemplateString, Object redisTemplate new RedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } } 存入Long对象取出Integer对象
测试方法如下 Test public void redisSerializerLong(){ try { Long longValue 123L; redisLongCache.set(cacheLongValue,longValue); Object cacheValue redisLongCache.get(cacheLongValue); Long a (Long) cacheValue; }catch (ClassCastException e){ e.printStackTrace(); } } 会报类型转换错误java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long。
为什么类型会变为Integer呢跟我一起追踪源码便会发现问题。
1. 在代码的最外层获取redis中key对应的value值
redisTemplate.opsForValue().get(key);
2.在DefaultValueOperations类中的get(Object key)方法 public V get(Object key) { return execute(new ValueDeserializingRedisCallback(key) { Override protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { return connection.get(rawKey); } }, true); } 3.打断点继续往里跟RedisTemplate中的execute(RedisCallbackT action, boolean exposeConnection, boolean pipeline)方法里面有一行关键代码。
T result action.doInRedis(connToExpose);
此为获取redis中对应的value值并对其进行反序列化操作。
4.在抽象类AbstractOperationsK, V中定义了反序列化操作对查询结果result进行反序列化。 public final V doInRedis(RedisConnection connection) { byte[] result inRedis(rawKey(key), connection); return deserializeValue(result); } V deserializeValue(byte[] value)反序列化 V deserializeValue(byte[] value) { if (valueSerializer() null) { return (V) value; } return (V) valueSerializer().deserialize(value); } 5.终于到了具体实现类GenericJackson2JsonRedisSerializer public Object deserialize(Nullable byte[] source) throws SerializationException { return deserialize(source, Object.class); } 实现反序列化方法注意这里统一将结果反序列化为Object类型所以这里便是问题的根源所在对于数值类型取出后统一转为Object,导致泛型类型丢失数值自动转为了Integer类型也就不奇怪了。 public T T deserialize(Nullable byte[] source, ClassT type) throws SerializationException { Assert.notNull(type, Deserialization type must not be null! Pleaes provide Object.class to make use of Jackson2 default typing.); if (SerializationUtils.isEmpty(source)) { return null; } try { return mapper.readValue(source, type); } catch (Exception ex) { throw new SerializationException(Could not read JSON: ex.getMessage(), ex); } } String对象转义问题
测试方法 Test public void redisSerializerString() { try { String stringValue abc; redisStringCache.set(codeStringValue, stringValue); String cacheValue redisStringCache.get(codeStringValue); // 序列化失败 String serverInsert redisStringCache.get(serverInsertValue); if (Objects.equals(cacheValue, serverInsert)) { System.out.println(serializer ok); } else { System.out.println(serializer err); } } catch (Exception e) { e.printStackTrace(); } } 提前在redis服务器上插入一个非Json格式的String对象
直接在Redis服务器上使用set命令新增一对Key-Value在代码中取出会反序列化失败 org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized token abc: was expecting (true, false or null) at [Source: (byte[])abc; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token abc: was expecting (true, false or null) at [Source: (byte[])abc; line: 1, column: 7] at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:132) at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:110) at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:334) at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:60) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184) at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95) at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:48) 小总结
这个问题是因为自己在测试的过程中没有按照代码流程执行想当然的认为代码跑出来的结果和自己手动插入的结果是一样的。
在相关的测试验证过程中应该严格的控制变量不能凭借下意识的决断来操作谨记软件之事——必作于细 ———————————————— 文章转载自: http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.nchsz.cn.gov.cn.nchsz.cn http://www.morning.pbbzn.cn.gov.cn.pbbzn.cn http://www.morning.kmqwp.cn.gov.cn.kmqwp.cn http://www.morning.jkpnm.cn.gov.cn.jkpnm.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.mdmc.cn.gov.cn.mdmc.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.ysbhj.cn.gov.cn.ysbhj.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn