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

简介: 【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;






目录
相关文章
|
12月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
600 1
|
12月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
524 1
|
XML JSON 编解码
从JSON到Protobuf,深入序列化方案的选型与原理
序列化是数据跨边界传输的“翻译官”,将结构化数据转为二进制流。JSON可读性强但冗余大,Protobuf高效紧凑、性能优越,成主流选择。不同场景需权衡标准化与定制优化,选最合适方案。
710 3
|
JSON 人工智能 Go
在Golang中序列化JSON字符串的教程
在Golang中,使用`json.Marshal()`可将数据结构序列化为JSON格式。若直接对JSON字符串进行序列化,会因转义字符导致错误。解决方案包括使用`[]byte`或`json.RawMessage()`来避免双引号被转义,从而正确实现JSON的序列化与反序列化。
798 7
|
XML JSON Java
go语言之JSON序列化
本文介绍了Go语言中的JSON序列化与反序列化,其操作与Java类似。需要注意的是,由于Go语言的包管理机制,变量和引入包的首字母需大写,以便其他包引用。示例代码展示了如何将`Student`结构体进行JSON序列化(返回字节数组,需转为字符串)及反序列化。此外,文章还说明了通过tag(如`json`和`xml`)指定序列化变量的重要性,以避免因包间访问限制导致反序列化失败或值为null的问题。
284 0
|
JSON JavaScript 前端开发
Go语言JSON 序列化与反序列化 -《Go语言实战指南》
本文介绍了 Go 语言中使用 `encoding/json` 包实现 JSON 与数据结构之间的转换。内容涵盖序列化(`Marshal`)和反序列化(`Unmarshal`),包括基本示例、结构体字段标签的使用、控制字段行为的标签(如 `omitempty` 和 `-`)、处理 `map` 和切片、嵌套结构体序列化、反序列化未知结构(使用 `map[string]interface{}`)以及 JSON 数组的解析。最后通过表格总结了序列化与反序列化的方法及类型要求,帮助开发者快速掌握 JSON 数据处理技巧。
|
JSON JavaScript 前端开发
Go语言中json序列化的一个小坑,建议多留意一下
在Go语言开发中,JSON因其简洁和广泛的兼容性而常用于数据交换,但其在处理数字类型时存在精度问题。本文探讨了JSON序列化的一些局限性,并介绍了两种替代方案:Go特有的gob二进制协议,以及msgpack,两者都能有效解决类型保持和性能优化的问题。
679 7
|
JSON 前端开发 JavaScript
聊聊 Go 语言中的 JSON 序列化与 js 前端交互类型失真问题
在Web开发中,后端与前端的数据交换常使用JSON格式,但JavaScript的数字类型仅能安全处理-2^53到2^53间的整数,超出此范围会导致精度丢失。本文通过Go语言的`encoding/json`包,介绍如何通过将大整数以字符串形式序列化和反序列化,有效解决这一问题,确保前后端数据交换的准确性。
629 4
|
JSON JavaScript Java
对比JSON和Hessian2的序列化格式
通过以上对比分析,希望能够帮助开发者在不同场景下选择最适合的序列化格式,提高系统的整体性能和可维护性。
694 3
|
JSON 数据格式 索引
Python中序列化/反序列化JSON格式的数据
【11月更文挑战第4天】本文介绍了 Python 中使用 `json` 模块进行序列化和反序列化的操作。序列化是指将 Python 对象(如字典、列表)转换为 JSON 字符串,主要使用 `json.dumps` 方法。示例包括基本的字典和列表序列化,以及自定义类的序列化。反序列化则是将 JSON 字符串转换回 Python 对象,使用 `json.loads` 方法。文中还提供了具体的代码示例,展示了如何处理不同类型的 Python 对象。
1002 1

热门文章

最新文章