赞
踩
目录
最近跟着B站大佬青空敲代码,在使用rabbitmq发送邮件验证码时遇到了问题,控制台疯狂报错。
- org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
- at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:158) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1663) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1582) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1570) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1561) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListenerAndHandleException(AbstractMessageListenerContainer.java:1506) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.lambda$executeListener$8(AbstractMessageListenerContainer.java:1484) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at io.micrometer.observation.Observation.lambda$observe$0(Observation.java:493) ~[micrometer-observation-1.11.5.jar:1.11.5]
- at io.micrometer.observation.Observation.observeWithContext(Observation.java:603) ~[micrometer-observation-1.11.5.jar:1.11.5]
- at io.micrometer.observation.Observation.observe(Observation.java:492) ~[micrometer-observation-1.11.5.jar:1.11.5]
- at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1484) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:994) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:941) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1323) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1225) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
- Caused by: java.lang.SecurityException: Attempt to deserialize unauthorized class java.util.CollSer; add allowed class name patterns to the message converter or, if you trust the message orginiator, set environment variable 'SPRING_AMQP_DESERIALIZATION_TRUST_ALL' or system property 'spring.amqp.deserialization.trust.all' to true
- at org.springframework.amqp.utils.SerializationUtils.checkAllowedList(SerializationUtils.java:165) ~[spring-amqp-3.0.10.jar:3.0.10]
- at org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter.checkAllowedList(AllowedListDeserializingMessageConverter.java:61) ~[spring-amqp-3.0.10.jar:3.0.10]
- at org.springframework.amqp.support.converter.SimpleMessageConverter$1.resolveClass(SimpleMessageConverter.java:151) ~[spring-amqp-3.0.10.jar:3.0.10]
- at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2045) ~[na:na]
- at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1909) ~[na:na]
- at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235) ~[na:na]
- at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1744) ~[na:na]
- at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:514) ~[na:na]
- at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:472) ~[na:na]
- at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:102) ~[spring-amqp-3.0.10.jar:3.0.10]
- at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:83) ~[spring-amqp-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:342) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter$MessagingMessageConverterAdapter.extractPayload(MessagingMessageListenerAdapter.java:385) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:132) ~[spring-amqp-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:258) ~[spring-rabbit-3.0.10.jar:3.0.10]
- at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:148) ~[spring-rabbit-3.0.10.jar:3.0.10]
- ... 15 common frames omitted
-
-
- 进程已结束,退出代码为 -1
-
生成者代码:
- @Override
- public String registerEmailVerifyCode(String type, String email, String ip) {
- synchronized (ip.intern()){
- if (!this.verifyLimit(ip)) return "请求频繁,请稍后再试";
- Random random = new Random();
- int code = random.nextInt(899999) + 100000;
- Map<String, Object> data = Map.of("type", type, "email", email, "code", code);
- amqptemplate.convertAndSend("mail",data);
- stringRedisTemplate.opsForValue().set(Const.VERIFY_EMAIL_DATA + email,
- String.valueOf(code), 3, TimeUnit.MINUTES);
- return null;
- }
- }
消费者代码:
- @RabbitHandler
- public void sendMailMessage(Map<String, Object> data){
- String email = (String) data.get("email");
- Integer code = (Integer) data.get("code");
- String type = (String) data.get("type");
- SimpleMailMessage message = switch (type){
- case "register" ->
- createMessage("注册", "您的验证码为:" + code + "有效时间为一分钟,请勿向他人泄露验证码", email);
- case "reset" ->
- createMessage("重置密码", "您的验证码为:" + code + "有效时间为一分钟,请勿向他人泄露验证码", email);
- default -> null;
- };
- if (message == null) return;
- sender.send(message);
- }
rabbitmq配置代码:
- @Bean("emailQueue")
- public Queue emailQueue(){
- return QueueBuilder
- .durable("mail")
- .build();
- }
报错原因是因为生产者,消费者格式序列化,通过导入依赖
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- </dependency>
以及在rabbitmq配置类中添加Bean可以解决
- @Bean
- public MessageConverter jsonMessageConverter(){
- return new Jackson2JsonMessageConverter();
- }
解决方法引用了https://blog.csdn.net/Ammw_13/article/details/134586936。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。