Spring-boot-redis Date 데이터 유형 시리얼화

2372 단어 이상 집합
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not deserialize value of type java.util.Date from String “2017-8-30 12:00:00”: not a valid representation (error: Failed to parse Date value ‘2017-8-30 12:00:00’: Unparseable date: “2017-8-30 12:00:00”)
@Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        /**
         * om.setDateFormat(DateFormat.getDateTimeInstance());//     
         *                , Windows  Linux               ,
         *   DateFormat.getDateTimeInstance()                。
         * Windows:2017-8-30 12:00:00
         * Linux:Aug 30, 2017 12:00:00 PM
         *          :
         *       Windows      , Linux          ,        
         *          :
         *      org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not deserialize value of type java.util.Date from String "2017-8-30 12:00:00":
         *           not a valid representation (error: Failed to parse Date value '2017-8-30 12:00:00': Unparseable date: "2017-8-30 12:00:00")
         *      :
         *  om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
         */
//        om.setDateFormat(DateFormat.getDateTimeInstance());//     
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

좋은 웹페이지 즐겨찾기