修改 rabbitmq 序列化方式,使用 Jackson2JsonMessageConverter 替代

This commit is contained in:
YunaiV 2024-04-16 09:17:11 +08:00
parent bf70707719
commit 1d239c9877

View File

@ -1,12 +1,11 @@
package cn.iocoder.yudao.framework.mq.rabbitmq.config; package cn.iocoder.yudao.framework.mq.rabbitmq.config;
import cn.hutool.core.util.ReflectUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.utils.SerializationUtils; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import java.lang.reflect.Field;
/** /**
* RabbitMQ 消息队列配置类 * RabbitMQ 消息队列配置类
@ -18,12 +17,12 @@ import java.lang.reflect.Field;
@ConditionalOnClass(name = "org.springframework.amqp.rabbit.core.RabbitTemplate") @ConditionalOnClass(name = "org.springframework.amqp.rabbit.core.RabbitTemplate")
public class YudaoRabbitMQAutoConfiguration { public class YudaoRabbitMQAutoConfiguration {
static { /**
// 强制设置 SerializationUtils TRUST_ALL true避免 RabbitMQ Consumer 反序列化消息报错 * Jackson2JsonMessageConverter Bean使用 jackson 序列化消息
// 为什么不通过设置 spring.amqp.deserialization.trust.all 因为可能在 SerializationUtils static 初始化后 */
Field trustAllField = ReflectUtil.getField(SerializationUtils.class, "TRUST_ALL"); @Bean
ReflectUtil.removeFinalModify(trustAllField); public MessageConverter createMessageConverter() {
ReflectUtil.setFieldValue(SerializationUtils.class, trustAllField, true); return new Jackson2JsonMessageConverter();
} }
} }