springboot项目中使用redis对数据进行解析报Invalid numeric value: Leading zeroes not allowed错误
2019-03-23 13:54
591 查看
错误分析
在使用redis获取发送的短信验证码,偶尔会报如下错误信息
2019-01-15 10:32:15.104 [http-nio-8080-exec-10] WARN org.springframework.web.servlet.PageNotFound:1176 - No mapping found f lowed at [Source: [B@2836aebe; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid numeric value: Leading zeroes not allowed at [Source: [B@2836aebe; line: 1, column: 2] at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:73) at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:318) at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:58) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:207) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)
通过看日志发现是因为使用的jackson解析redis中存储的数据,会把纯数字的短信验证码当作是数字类型来解析,之所以偶尔会报错是因为生成的随机数验证码中可能会以0开头,导致jackson认为这是一个无效的数字才报的错,然后使用
RedisTemplate<String, String>当作字符串的方式获取短信验证码仍然不行,还是使用的jackson解析短信验证码,检测redis的配置发现是因为项目中指定的redisTemplate的数据解析方式为jackson解析
解决办法:
再配置一个StringRedisTemplate的redis模板:
@Bean(name = "stringRedisTemplate") public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){ return new StringRedisTemplate(redisConnectionFactory); }
使用方法:
- 在获取短信验证码的时候stringRedisTemplate来获取,这样就会把短信验证码通过字符串的方式解析,以0开头也不会报解析错误的问题
相关文章推荐
- SpringBoot项目中使用MongoDB充当数据库以及进行数据的增删查改的操作
- 使用Redis作为SpringBoot项目数据缓存
- Spring boot项目 使用ip+port+contextPath进行访问的时候会直接显示出项目中的一些接口信息和相关数据
- 4 Springboot中使用redis存储集合数据,并模拟条件查询、分页读取
- Spring Boot项目使用Eclipse进行断点调试Debug
- spring-boot项目中使用logback进行日志记录
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- SpringBoot项目使用Redis
- spring boot使用fastJson解析数据
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- Spring Boot完美使用FastJson解析JSON数据(十三)
- Spring Boot使用Redis进行消息的发布订阅
- 使用IDEA创建springboot项目时,出现错误Cannot download 'https://start.spring.io': connect timed out
- Spring Boot 完美使用FastJson解析Json数据
- Spring Boot使用FastJson解析JSON数据的方法
- springboot整合redis进行数据操作(推荐)
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- Spring Boot 实践2 --项目中使用 Redis
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法