【redis序列化】配置Jedis不需要序列化存入json 序列化Joda DateTime

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Redis 版,经济版 1GB 1个月
简介: 【redis序列化】配置Jedis不需要序列化存入json 序列化Joda DateTime

序列化image.png


反序列化

image.png

redis配置信息

image.png

objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);增加显示类名信息知道其属于哪个类






package cn.duckerkj.springbootguide.config.redis;

import org.joda.time.DateTime;

import  org.springframework.cache.annotation.CachingConfigurerSupport;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import  org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.StringRedisTemplate;

import  org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import  org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.module.SimpleModule;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

@Configuration

@EnableCaching

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)

public class CacheConfig extends CachingConfigurerSupport {

   /**

    * RedisTemplate配置

    * @param factory

    * @return

    */

   @Bean

   public RedisTemplate redisTemplate(RedisConnectionFactory  factory) {

       StringRedisTemplate template = new  StringRedisTemplate(factory);

       //定义key序列化方式

       //RedisSerializer<String> redisSerializer = new  StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要

       //定义value的序列化方式

       Jackson2JsonRedisSerializer jackson2JsonRedisSerializer  = new Jackson2JsonRedisSerializer(Object.class);

       ObjectMapper om = new ObjectMapper();

       

       SimpleModule simpleModule = new SimpleModule();

       simpleModule.addSerializer(DateTime.class,new  JodaDateTimeJsonSerializer());

       simpleModule.addDeserializer(DateTime.class,new  JodaDateTimeJsonDeserializer());

       om.registerModule(simpleModule);

       

        om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);

        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

       jackson2JsonRedisSerializer.setObjectMapper(om);

        template.setValueSerializer(jackson2JsonRedisSerializer);

        template.setHashValueSerializer(jackson2JsonRedisSerializer);

       template.afterPropertiesSet();

       return template;

   }

   

   

   /*@Bean

   public RedisTemplate redisTemplate(RedisConnectionFactory  redisConnectionFactory){

       RedisTemplate redisTemplate = new RedisTemplate();

        redisTemplate.setConnectionFactory(redisConnectionFactory);

       //首先解决key的序列化方式

       StringRedisSerializer stringRedisSerializer = new  StringRedisSerializer();

       redisTemplate.setKeySerializer(stringRedisSerializer);

       //解决value的序列化方式

       Jackson2JsonRedisSerializer jackson2JsonRedisSerializer  = new Jackson2JsonRedisSerializer(Object.class);

       ObjectMapper objectMapper =  new ObjectMapper();

       SimpleModule simpleModule = new SimpleModule();

       simpleModule.addSerializer(DateTime.class,new  JodaDateTimeJsonSerializer());

       simpleModule.addDeserializer(DateTime.class,new  JodaDateTimeJsonDeserializer());

        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

       objectMapper.registerModule(simpleModule);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

       return redisTemplate;

   }*/

}






package cn.duckerkj.springbootguide.config.redis;



import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.DeserializationContext;

import com.fasterxml.jackson.databind.JsonDeserializer;

import org.joda.time.DateTime;

import org.joda.time.format.DateTimeFormat;

import org.joda.time.format.DateTimeFormatter;



import java.io.IOException;



/**

* Created by hzllb on 2019/2/14.

*/

public class JodaDateTimeJsonDeserializer extends JsonDeserializer<DateTime> {

   @Override

   public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

       String dateString =jsonParser.readValueAs(String.class);

       DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");



       return DateTime.parse(dateString,formatter);

   }

}





package cn.duckerkj.springbootguide.config.redis;



import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.joda.time.DateTime;



import java.io.IOException;



/**

* Created by hzllb on 2019/2/14.

*/

public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {

   @Override

   public void serialize(DateTime dateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

       jsonGenerator.writeString(dateTime.toString("yyyy-MM-dd HH:mm:ss"));

   }

}



需要增加

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency>



自己手动import com.fasterxml.jackson.annotation.JsonAutoDetect;






相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
1月前
|
存储 JSON NoSQL
JSON 存入 Redis
【7月更文挑战第8天】
50 12
|
30天前
|
JSON Dart 安全
Flutter Dart Macro 宏简化 JSON 序列化
今天我们将会体验 dart 语言新特性 macro 宏,来实现对 json 的序列化,用到的包是官方实验室写的 json 包。 本文将会一步步的带你实现这个功能,那我们开始吧。
Flutter Dart Macro 宏简化 JSON 序列化
|
18天前
|
XML 存储 JSON
(十二)探索高性能通信与RPC框架基石:Json、ProtoBuf、Hessian序列化详解
如今这个分布式风靡的时代,网络通信技术,是每位技术人员必须掌握的技能,因为无论是哪种分布式技术,都离不开心跳、选举、节点感知、数据同步……等机制,而究其根本,这些技术的本质都是网络间的数据交互。正因如此,想要构建一个高性能的分布式组件/系统,不得不思考一个问题:怎么才能让数据传输的速度更快?
|
4天前
|
JSON JavaScript 前端开发
Vue项目使用Cookie,以Json格式存入与读取Cookie,设置过期时间以及删除操作
这篇文章介绍了在Vue项目中如何使用JavaScript操作Cookie,包括设置、读取、设置过期时间以及删除Cookie的方法。
18 0
|
1月前
tasks.json、launch.json、c_cpp_properties.json配置
tasks.json、launch.json、c_cpp_properties.json配置
16 0
|
1月前
|
XML JSON 缓存
优化Java中XML和JSON序列化
优化Java中XML和JSON序列化
|
2月前
|
机器学习/深度学习 JSON 移动开发
详细解读BootStrap智能表单系列八表单配置json详解
详细解读BootStrap智能表单系列八表单配置json详解
14 0
|
2月前
|
存储 JSON NoSQL
深入解析RedisJSON:在Redis中直接处理JSON数据
深入解析RedisJSON:在Redis中直接处理JSON数据
|
1天前
|
JSON Java Android开发
Android 开发者必备秘籍:轻松攻克 JSON 格式数据解析难题,让你的应用更出色!
【8月更文挑战第18天】在Android开发中,解析JSON数据至关重要。JSON以其简洁和易读成为首选的数据交换格式。开发者可通过多种途径解析JSON,如使用内置的`JSONObject`和`JSONArray`类直接操作数据,或借助Google提供的Gson库将JSON自动映射为Java对象。无论哪种方法,正确解析JSON都是实现高效应用的关键,能帮助开发者处理网络请求返回的数据,并将其展示给用户,从而提升应用的功能性和用户体验。
|
4天前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
14 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧