springboot~jackson和redis日期序列化

日期类型,我们需要统一它在被序列化后的格式,序列化是指把java类型通过网络系统或者IO进行持久化,如把一个java对象保存到txt文件里,或者把java对象保存到redis里,又或者把java对象通过网络以JSON的格式输出到浏览器上。

jackson的日期序列化

    public class JacksonDateSerializerModifier extends BeanSerializerModifier {
        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            for (Object beanProperty : beanProperties) {
                BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty;
                Class<?> clazz = writer.getType().getRawClass();
                if (clazz.equals(Date.class)) {
                    writer.assignSerializer(new DateSerializer());
                }
            }
            return beanProperties;
        }
    
        /**
         * 格式化日期
         */
        public class DateSerializer extends JsonSerializer<Object> {
            @Override
            public void serialize(Object date, JsonGenerator jsonGenerator,
                                  SerializerProvider serializerProvider) throws IOException {
                if (date == null) {
                    jsonGenerator.writeNumber(StringUtils.EMPTY);
                }
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
                format.setTimeZone(timeZone);
                String dateString = "\"" + format.format(date) + "\"";
                jsonGenerator.writeNumber(dateString);
            }
        }
    }

通过redisTempleate输出到redis里

 @Bean(name = "redisTemplate")
    @ConditionalOnClass(RedisOperations.class)//依据RedisOperations是否存在而决定是否注册这个bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 日期序列化处理
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        om.registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule())
                .registerModule(new ParameterNamesModule());

        om.activateDefaultTyping(
                LaissezFaireSubTypeValidator.instance ,
                ObjectMapper.DefaultTyping.NON_FINAL,
                JsonTypeInfo.As.WRAPPER_ARRAY);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
posted @ 2022-04-16 22:43  张占岭  阅读(11)  评论(0编辑  收藏  举报