Redis消息队列,获取message.getBody(),有乱码问题解决。

想要做一个redis的消息队列。运行时发现message.getBody()存在乱码
�� t{"env":"hw","group":"job"}

首先做了一下内容

java 复制代码
// Bean类
@Data
public class ADto{
  private String env;
  private String group;
}
java 复制代码
// controller类
@RestController
public class testController{
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("测试redis消息队列")
    @PostMapping("/redisTest")
    public String redisTest(@RequestBody ADto adto){
        redisTemplate.convertAndSend("REDIS_TEST_CHANNEL", JSONObject.toJSONString(adto));
        return "发送成功!";
    }
}
java 复制代码
//Service 类
@Service
public class AService{
    public void warnNotice(ADto adto){
    // 具体处理逻辑
        System.println.out("打印:" + adto.toString());
    }
}
java 复制代码
// redis监听器
@Component
public class RedisConfig {
    @Bean
    public ChannelTopic topic() {
        return new ChannelTopic("WARN_NOTICE_CHANNEL");
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter(WarnListener listener) {
        return new MessageListenerAdapter(listener);
    }

    @Bean
    public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory,
                                                        MessageListenerAdapter messageListenerAdapter,
                                                        ChannelTopic topic) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(messageListenerAdapter, topic);
        return container;
    }

@Component
public class WarnListener implements MessageListener {

    @Resource
    private AExtService AExtService;

    private final Object lock = new Object();

    @Override
    public void onMessage(Message message, byte[] pattern) {
        synchronized(lock) {
            ADto dto = JSONObject.parseObject(message.getBody(), ADto .class);
            AExtService.warnNotice(dto);
        }

    }
}

运行启动以后,代码在ADto dto = JSONObject.parseObject(message.getBody(), ADto .class);这一行,直接执行结束,打断点以后发现,message.getBody()获取到的内容,不是一个普通的json,而是有乱码的

�� t{"env":"hw","group":"job"}

解决方案:

@Resource

private RedisTemplate redisTemplate;

修改为

@Resource

private StringRedisTemplate stringRedisTemplate;

相关推荐
陈阿土i2 小时前
SpringAI 1.0.0 正式版——利用Redis存储会话(ChatMemory)
java·redis·ai·springai
bing_1583 小时前
跨多个微服务使用 Redis 共享数据时,如何管理数据一致性?
redis·微服务·mybatis
多多*3 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap
HAPPY酷5 小时前
Kafka 和Redis 在系统架构中的位置
redis·kafka·系统架构
gaoliheng0066 小时前
Redis看门狗机制
java·数据库·redis
潘yi.7 小时前
Redis哨兵模式
数据库·redis·缓存
瀚海澜生8 小时前
redis系列(1)——redis高效的本质:基础键值对的组织和基础数据结构
redis
努力学习的小廉8 小时前
我爱学算法之—— 前缀和(中)
开发语言·redis·算法
多多*9 小时前
基于rpc框架Dubbo实现的微服务转发实战
java·开发语言·前端·redis·职场和发展·蓝桥杯·safari
MuYiLuck13 小时前
【redis实战篇】第八天
数据库·redis·缓存