更多ruoyi-nbcio功能请看演示系统
gitee源代码地址
前后端代码: https://gitee.com/nbacheng/ruoyi-nbcio
演示地址:RuoYi-Nbcio后台管理系统
发送websoket消息的时候后在redis监听出现下面错误
说明是RedisTemplate的反json序列化出现问题
RedisTemplate进行JSON的序列化器配置,如下:
/** * RedisTemplate配置 * @param redisConnectionFactory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 创建RedisTemplate对象 RedisTemplate<String, Object> template = new RedisTemplate<>(); // 设置Redis连接工厂 template.setConnectionFactory(redisConnectionFactory); // 创建JSON序列化器(两种设置Value和HashValue的JSON的序列化器) Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); // 设置Key和HashKey采用String序列化 template.setKeySerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); // 设置Value和HashValue采用JSON的序列化 template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); //template.setValueSerializer(jackson2JsonRedisSerializer); //template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; }
有出现下面的错误
2023-12-22 13:08:14[0;39m [32m[redisContainer-2][0;39m [1;31mERROR[0;39m [1;35mo.s.d.r.l.a.MessageListenerAdapter [0;39m - Listener execution failed org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object at [Source: (byte[])"{"handlerName":"socketHandler","message":"{\"msgId\":1738063877575565314,\"cmd\":\"user\",\"msgTxt\":\"流程待办通知\",\"userId\":\"admin\"}","userId":"admin"}"; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object at [Source: (byte[])"{"handlerName":"socketHandler","message":"{\"msgId\":1738063877575565314,\"cmd\":\"user\",\"msgTxt\":\"流程待办通知\",\"userId\":\"admin\"}","userId":"admin"}"; line: 1, column: 1] at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:75) at org.springframework.data.redis.listener.adapter.MessageListenerAdapter.extractMessage(MessageListenerAdapter.java:342) at org.springframework.data.redis.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:302) at org.springframework.data.redis.listener.RedisMessageListenerContainer.processMessage(RedisMessageListenerContainer.java:845) at org.springframework.data.redis.listener.RedisMessageListenerContainer.lambda$dispatchMessage$7(RedisMessageListenerContainer.java:993) at java.lang.Thread.run(Thread.java:745)
那就发送消息的时候就进行json处理
如下:
public void sendMessage(String handlerName, BaseMap params) throws JsonProcessingException { params.put(GlobalConstants.HANDLER_NAME, handlerName); ObjectMapper objectMapper = new ObjectMapper(); // 将HashMap转换为JSON字符串 String json = objectMapper.writeValueAsString(params); redisTemplate.convertAndSend(GlobalConstants.REDIS_TOPIC_NAME, json); }