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

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


反序列化

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

redis配置信息

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

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;






上一篇:linux基础之文件编辑


下一篇:你还在使用xshell绿色破解版?