11package com .projetopit .backend .config ;
22
3+ import com .fasterxml .jackson .annotation .JsonTypeInfo ;
4+ import com .fasterxml .jackson .databind .DeserializationFeature ;
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import com .fasterxml .jackson .databind .SerializationFeature ;
7+ import com .fasterxml .jackson .databind .jsontype .BasicPolymorphicTypeValidator ;
8+ import com .fasterxml .jackson .databind .jsontype .PolymorphicTypeValidator ;
9+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
10+ import org .jspecify .annotations .NonNull ;
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
13+ import org .springframework .cache .Cache ;
14+ import org .springframework .cache .annotation .CachingConfigurer ;
315import org .springframework .cache .annotation .EnableCaching ;
16+ import org .springframework .cache .interceptor .CacheErrorHandler ;
417import org .springframework .context .annotation .Bean ;
518import org .springframework .context .annotation .Configuration ;
619import org .springframework .data .redis .cache .RedisCacheConfiguration ;
922import org .springframework .data .redis .serializer .GenericJackson2JsonRedisSerializer ;
1023import org .springframework .data .redis .serializer .RedisSerializationContext ;
1124
12- import com .fasterxml .jackson .databind .ObjectMapper ;
13- import com .fasterxml .jackson .databind .SerializationFeature ;
14- import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
15-
1625import java .time .Duration ;
1726
1827@ Configuration
1928@ EnableCaching
20- public class RedisConfig {
29+ public class RedisConfig implements CachingConfigurer {
30+
31+ private static final Logger log = LoggerFactory .getLogger (RedisConfig .class );
2132
2233 @ Bean
2334 public RedisCacheManager cacheManager (RedisConnectionFactory connectionFactory ) {
24- ObjectMapper objectMapper = new ObjectMapper ();
25- objectMapper .registerModule (new JavaTimeModule ());
26- objectMapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
27- objectMapper .activateDefaultTyping (objectMapper .getPolymorphicTypeValidator (), ObjectMapper .DefaultTyping .NON_FINAL );
28- GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer (objectMapper );
35+ ObjectMapper mapper = new ObjectMapper ();
36+
37+ mapper .registerModule (new JavaTimeModule ());
38+ mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
39+ mapper .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
40+ mapper .disable (SerializationFeature .FAIL_ON_EMPTY_BEANS );
41+
42+ // Validador de Tipos Seguro
43+ PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator .builder ()
44+ .allowIfBaseType (Object .class )
45+ .build ();
46+
47+ // CORREÇÃO: Usamos NON_FINAL em vez do EVERYTHING depreciado
48+ mapper .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .NON_FINAL , JsonTypeInfo .As .PROPERTY );
49+
50+ // Mantemos o serializer do Jackson 2, mas usamos o padrão Builder recomendado
51+ GenericJackson2JsonRedisSerializer serializer = GenericJackson2JsonRedisSerializer .builder ()
52+ .objectMapper (mapper )
53+ .build ();
2954
3055 RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration .defaultCacheConfig ()
31- .entryTtl (Duration .ofHours (1 )) // Cache entities for 1 Hour by default
56+ .entryTtl (Duration .ofHours (1 ))
3257 .disableCachingNullValues ()
3358 .serializeValuesWith (RedisSerializationContext .SerializationPair .fromSerializer (serializer ));
3459
3560 return RedisCacheManager .builder (connectionFactory )
3661 .cacheDefaults (defaultCacheConfig )
3762 .build ();
3863 }
39- }
64+
65+ @ Override
66+ public CacheErrorHandler errorHandler () {
67+ return new CacheErrorHandler () {
68+ @ Override
69+ public void handleCacheGetError (@ NonNull RuntimeException e , @ NonNull Cache cache , @ NonNull Object key ) {
70+ log .warn ("Cache GET erro em '{}' chave='{}'. Causa: " , cache .getName (), key , e .getMessage ());
71+ }
72+ @ Override
73+ public void handleCachePutError (@ NonNull RuntimeException e , @ NonNull Cache cache , @ NonNull Object key , Object value ) {
74+ log .warn ("Cache PUT erro em '{}' chave='{}'. Causa: " , cache .getName (), key , e .getMessage ());
75+ }
76+ @ Override
77+ public void handleCacheEvictError (@ NonNull RuntimeException e , @ NonNull Cache cache , @ NonNull Object key ) {
78+ log .warn ("Cache EVICT erro em '{}' chave='{}'. Causa: " , cache .getName (), key , e .getMessage ());
79+ }
80+ @ Override
81+ public void handleCacheClearError (@ NonNull RuntimeException e , @ NonNull Cache cache ) {
82+ log .warn ("Cache CLEAR erro em '{}'. Causa: " , cache .getName (), e .getMessage ());
83+ }
84+ };
85+ }
86+ }
0 commit comments