无法将消息对象从 RabbitMQ 转换为 java 类
Posted
技术标签:
【中文标题】无法将消息对象从 RabbitMQ 转换为 java 类【英文标题】:Can't convert Message object from RabbitMQ to java class 【发布时间】:2019-05-01 23:03:07 【问题描述】:我创建了我的 RabbitListener 来从 RabbitMQ 队列中获取消息。
我的 rabbitMQ 消息:
Properties
priority: 0
delivery_mode: 2
headers:
__TypeId__: com.kmb.bank.models.Transfer
content_encoding: UTF-8
content_type: application/json
Payload
356 bytes
Encoding: string
"userAccountNumber":"1111444422221111","title":"123","recipientName":"123","recipientAccountNumber":"1234123412341234","amount":123.0,"localDateTime":"nano":526106200,"year":2018,"monthValue":11,"dayOfMonth":29,"hour":20,"minute":43,"second":0,"month":"NOVEMBER","dayOfWeek":"THURSDAY","dayOfYear":333,"chronology":"id":"ISO","calendarType":"iso8601"
我的监听器方法:
@Autowired
private Jackson2JsonMessageConverter jackson2JsonMessageConverter;
@RabbitListener(queues = "kolejka")
public void listen(Message message)
try
Transfer transfer = (Transfer) jackson2JsonMessageConverter.fromMessage(message);
log.info(transfer);
catch (Exception e)
log.debug("Error thrown while listening + " + e.getMessage());
豆配置: @豆角,扁豆 公共 ObjectMapper objectMapper() 返回新的对象映射器();
@Bean
public Jackson2JsonMessageConverter jackson2JsonMessageConverter()
return new Jackson2JsonMessageConverter(objectMapper());
还有转学类:
package com.kmb.transactionlogger.models;
@AllArgsConstructor
public class Transfer
@Getter @Setter
private String userAccountNumber;
@Getter @Setter
private String title;
@Getter @Setter
private String recipientName;
@Getter @Setter
private String recipientAccountNumber;
@Getter @Setter
private double amount;
@Getter @Setter
private LocalDateTime localDateTime;
不幸的是,在从 Message 转换为 Transfer 传输对象时引发了异常。
Caused by: org.springframework.amqp.support.converter.MessageConversionException: failed to resolve class name. Class not found [com.kmb.bank.models.Transfer]
2018-11-29 20:47:01.615 WARN 13688 --- [cTaskExecutor-1] ingErrorHandler$DefaultExceptionStrategy : Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: (Body:'"userAccountNumber":"1111444422221111","title":"123","recipientName":"123","recipientAccountNumber":"1234123412341234","amount":123.0,"localDateTime":"nano":599669800,"year":2018,"monthValue":11,"dayOfMonth":29,"hour":20,"minute":47,"second":1,"month":"NOVEMBER","dayOfWeek":"THURSDAY","dayOfYear":333,"chronology":"id":"ISO","calendarType":"iso8601"' MessageProperties [headers=__TypeId__=com.kmb.bank.models.Transfer, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=bank, receivedRoutingKey=, deliveryTag=2, consumerTag=amq.ctag-sLNqW-_WhDWLWJk6MCQcjg, consumerQueue=kolejka])
整个消息日志: https://pastebin.com/raw/47Lq7dYD
【问题讨论】:
能否提供更详细的日志(stacktrace)? 我认为是Transfer类不同,包与TypeId不同的问题,但我不知道解决方案 您必须在接收jackson2JsonMessageConverter
的类型映射器中设置类型映射以映射到不同的类。或者,如果您使用public void listen(Transfer transfer)
,如果您将转换器连接到侦听器容器工厂,框架将从参数推断类型。如果它是 Boot 应用程序,则会自动发生。
【参考方案1】:
您必须在接收Jackson2JsonMessageConverter
的类型映射器中设置类型映射以映射到不同的类。
通常,发送者会将其类映射到令牌,例如transfer
,接收者会将其映射到其版本的Transfer
。
或者,如果您使用public void listen(Transfer transfer)
,并且如果您将转换器连接到侦听器容器工厂,框架将根据参数推断类型。
如果它是一个 Spring Boot 应用程序,该连接将自动发生。
【讨论】:
它有点帮助,但现在我遇到与 LocalDateTime 相关的错误:原因:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造java.time.LocalDateTime
的实例(没有创作者,比如默认构造,存在):无法从对象值反序列化(没有基于委托或基于属性的创建者)在
您需要将自定义 Jackson 反序列化器应用于 ObjectMapper - 这是一个可以回答的问题:***.com/questions/29956175/…以上是关于无法将消息对象从 RabbitMQ 转换为 java 类的主要内容,如果未能解决你的问题,请参考以下文章
rabbitmq 消费 json 消息并转换成 Java 对象
Spring Boot - RabbitMQ - 将消息内容类型从八位字节流更改为 json
从sql table处理PSCustomObject中的空值。无法转换对象
Firebase 函数 - 发送推送并收到错误消息“TypeError:无法将未定义或 null 转换为对象”[关闭]