[Redis 에러] cannot be cast to class java.lang.String ( is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')

탱귤생귤·2024년 2월 22일

ERROR

목록 보기
21/33

어떤 클래스를 레디스에 저장하려고 했을 때 떴던 에러다. 여러 곳을 찾다가

public static void main(String[] args) {
    System.setProperty("spring.devtools.restart.enabled", "false");
    SpringApplication.run(MyApp.class, args);
}

이걸 추가하면 된대서 했더니 이상한 오류만 더 나고 해결되지 않았다.

결국 더 찾고 찾다가 해결했다.
Transaction이 안돼서 뜬다는 말을 보고 추가한 코드다.

// RedisConfig
@Bean
   public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

       RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
       redisTemplate.setConnectionFactory(redisConnectionFactory);
       redisTemplate.setEnableTransactionSupport(true);
       redisTemplate.setKeySerializer(new StringRedisSerializer());
       redisTemplate.setValueSerializer(new StringRedisSerializer());
       return redisTemplate;
   }

redisTemplate.setEnableTransactionSupport(true); 이거 넣으니까 됐다. 그러나 이 한 줄의 코드는 엄청난 영향을 미치는데...

class EventServiceImplTest {

   @Autowired
   UserRepository userRepository;
   @Autowired
   EventRepository eventRepository;
   @Autowired
   EventRedisRepository redisRepository;
   @Autowired
   RedisTemplate<String, Object> redisTemplate;
   @Autowired
   ObjectMapper objectMapper;

   @PersistenceContext
   EntityManager entityManager;

   @Test
   void redis사진저장() throws JsonProcessingException {
       PhotoRedis photo1 = new PhotoRedis("1", "1L", "2L", "test1");
       PhotoRedis photo2 = new PhotoRedis("2", "1L", "2eL", "test1");

       redisTemplate.opsForList().leftPush("photos", objectMapper.writeValueAsString(photo1));
       redisTemplate.opsForList().leftPush("photos", objectMapper.writeValueAsString(photo2));
   }

redisTemplate 선언 시 <>를 추가하니까 됐다. 위의 Transcoding은 추가하지 않고 이것만 했어도 됐을 것이다. 왜냐하면 나중에 Transcoding 코드는 지웠기 때문이다.

0개의 댓글