今天在整合redis和spring boot的时候,遇到了一个错误,记录一下。

报错如下:

Could not read JSON: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

查看后发现是数据从redis中取出时,Jackson2反序列化数据处理LocalDateTime类型时出错,原因是:Jackson2在序列化LocalDateTime时输出的不是普通的字符串时间格式,而是如下所示的格式(普通时间格式:2019-02-27 12:10:17)

{
"date": {
"year": 2019,
"month": "FEBRUARY",
"day": 27,
"dayOfMonth": 27,
"dayOfWeek": "WEDNESDAY",
"era": [
"java.time.chrono.IsoEra",
"CE"
],
"dayOfYear": 58,
"leapYear": false,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
},
"prolepticMonth": 24229,
"monthValue": 2
},
"time": {
"hour": 12,
"minute": 10,
"second": 17,
"nano": 0
},
"month": "FEBRUARY",
"year": 2019,
"dayOfMonth": 27,
"dayOfWeek": "WEDNESDAY",
"dayOfYear": 58,
"hour": 12,
"minute": 10,
"nano": 0,
"second": 17,
"monthValue": 2,
"chronology": [
"java.time.chrono.IsoChronology",
{
"id": "ISO",
"calendarType": "iso8601"
}
]
}

所以是Jackson2序列化LocalDateTime跟我们所预想的不一致,将注册给redis的序列化模板修改成以下就行

 @Configuration
public class RedisConfig { /**
* 定义 UserRedisTemplate ,指定序列化和反序列化的处理类
*
* @param factory redis连接工厂
* @return 模板
*/
@Bean("UserRedisTemplate")
public RedisTemplate<String, User> userRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, User> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer<User> j2jrs = new Jackson2JsonRedisSerializer<>(User.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 解决jackson2无法反序列化LocalDateTime的问题
18 om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
19 om.registerModule(new JavaTimeModule());
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
j2jrs.setObjectMapper(om);
// 序列化 value 时使用此序列化方法
template.setValueSerializer(j2jrs);
template.setHashValueSerializer(j2jrs);
// 序列化 key 时
StringRedisSerializer srs = new StringRedisSerializer();
template.setKeySerializer(srs);
template.setHashKeySerializer(srs);
template.afterPropertiesSet();
return template;
}
}

主要是加上标红的那两句

参考文档:

GenericJackson2JsonRedisSerializer 反序列化问题

解决Jackson2反序列化LocalDateTime报错的更多相关文章

  1. 解决一个报表EdmFunction报错问题

        最近测试组提了一个bug,说是某个报表点击查询报错,查看错误log,错误信息如下. 类型"Ticket.Data.SqlFuns"上指定的方法"Boolean C ...

  2. 解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element

    解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element 'beans'.Referenced file conta ...

  3. Mysql备份迁移——MySqlBackup(.net)——(无法解决视图嵌视图报错)

    这里是利用MySqlBackup,可以再nuget中下载. 无法解决视图嵌视图报错的问题,只导表跟数据比较合适,如果有视图嵌视图,请参照Mysql备份迁移——Mysqldump(.NET调用Mysql ...

  4. 解决cookies存储中文报错问题

    URLEncoder.encode("username", "UTF-8"); URLDecoder.decode("123", " ...

  5. 解决element-ui upload组件报错 Avoid using non-primitive value as key, use string/number value instead

    到底是啥错呢,就是要求你的key必须是string或者number类型 那么解决就是找到这个报错的key(在node_modules/element-ui/lib/element-ui.common. ...

  6. 解决spring boot启动报错java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level

    解决spring boot启动报错java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level 学习了:https://blog.csdn. ...

  7. 解决centos7下 selenium报错--unknown error: DevToolsActivePort file doesn't exist

    解决centos7下 selenium报错--unknown error: DevToolsActivePort file doesn't exist 早上在linux下用selenium启动Chro ...

  8. 如何解决金蝶IKernel.exe报错 Windows Installer 错误 重新安装、无法卸载

    如何解决金蝶IKernel.exe报错 Windows Installer 错误 金蝶这个小婊子,就是这么贱. 卸载了高版本的,再安装低版本就不能安装上,死活都不能安装. 请手动启动一下Install ...

  9. 完美解决 scipy.misc.imread 报错 TypeError: Image data cannot be converted to float

    File "/home/harrison/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 634, ...

随机推荐

  1. Tronado【第1篇】:tronado的简单使用以及使用

    Tronado Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp ...

  2. ks代码助解

    代码实现: data_test_2 = {'gd':[1,1,1,1,1,1,0,0,0,0,0,0,0],'score':[1,2,0,2,2,7,4,5,4,0,4,18,np.nan]} dat ...

  3. k-means伪代码

    1.初始化k个簇中心. 2.更新所有样本点簇归属:样本点到哪个簇中心点最近就属于哪个簇. 3.重新计算每个簇的中心点(直到簇中心点不再变化或达到更新最大次数) #k-means伪代码 import n ...

  4. html button标签 语法

    html button标签 语法 button标签怎么用? 作用:定义一个按钮. 语法:<button type="button">按钮</button> ...

  5. CSS水印“点击穿透”

  6. T3

    T3构造图

  7. max pool实现

    题目 二维矩阵(nm) 求每个(lw)的子矩阵的最大元素, 就是一维滑动窗口的升级版 自己瞎掰的题解 #include <bits/stdc++.h> using namespace st ...

  8. Logger工具类

    org.slf4j.Logger的简单封装,传入所在类的class,和类名或全类名. public class LoggerFactory { public static Logger getLogg ...

  9. springboot(三).springboot用最简单的方式整合mybatis

     Springboot整合mybatis 在众多的orm框架中,我使用最多的,最习惯的,也是目前使用最广泛的就是mybatis,接下来我们就去将springboot整合mybatis 对于spring ...

  10. html初体验#1

    html的一些自见解 html 5 自带语义化,就是让标签带上感情色彩,方便人或浏览器区分 <b></b>与<strong></strong>的区别 & ...