Spring Redis pubsub: onMessage message.getBody() does not convert to correct String

356 Views Asked by At

I am trying to implement RedisSubscriber which implements MessageListener

@Override
public void onMessage(Message message, byte[] pattern) {

    messageList.add(message.getBody().toString());

    System.out.println("Message received: " + new String(message.getBody(), StandardCharsets.UTF_8));

}

But in my console it does not print the right string, it prints:

Message received: �� t Hi test

Why is it printing extra characters?

I tried deserializing it as well but it does not convert properly

1

There are 1 best solutions below

0
On

I solved it. Just add String Serializer in RedisConfig

@Bean
open fun redisTemplate(): RedisTemplate<String, Any> {
    val template = RedisTemplate<String, Any>()
    template.setConnectionFactory(jedisConnectionFactory())
    template.setDefaultSerializer(StringRedisSerializer())
    return template
}