当前位置: 首页 > 知识库问答 >
问题:

无法将消息对象从RabbitMQ转换为java类

平学
2023-03-14

我创建了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());
    }

}
@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 Transfer Object时引发了异常。

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

共有1个答案

阎冠玉
2023-03-14

您必须在接收Jackson2JSONMessageConverter的类型映射器中设置类型映射,以映射到不同的类。

通常,发送方将其类映射到令牌,例如transfer,而接收方将其映射到其transfer版本。

或者,如果您使用public void listent(Transfer Transfer),并且如果您将转换器连接到侦听器容器工厂,框架将从参数推断类型。

如果它是一个Spring Boot应用程序,布线将自动发生。

 类似资料:
  • 我想填充Javapojo类与mqtt消息有效载荷在一个Springmvc应用程序。我的代码是: MQTT MessagePayload是{“name”:“abc”,“age”:32},但当它在变量MessagePayload中转换为字符串时,它显示为类似{name:abc,age:32}。你看,去掉所有的双引号。当转到try块时,会抛出一个错误com。fasterxml。杰克逊。果心JsonPar

  • 我有一个 X509CertificateObject,一个匹配的 RSAPublicKey,并设法创建一个字节数组,其中包含某个消息对象的有效数字证书,也作为字节数组。 不幸的是,我正在构建的系统只接受对象作为输入。如何将我的基本构建块转换为这样一个有效的 对象? 背景:根据这个示例(摘要是SHA512),我正在试用Java Bouncy Castle RSA盲签名,需要将结果输入到标准签名处理中

  • 我无法将给定的Json字符串转换为java对象

  • 例如:Date值为:“dateCollected”:fri Jul 07 00:00:00 IST 1989, 但它只服用星期五而不是整个日期。

  • 我正在使用apache camel(Fuse 2.10.x)和soap over http和soap over JMS。JMS消息由对象消息转换为字节消息格式,这就造成了消息读取的问题。 我正在JBoss5.0GA环境中使用用于websphere MQ的JNDI连接。 我们遇到了IBM属性的另一个问题,通过删除属性解决了这个问题。我们还有camel header属性来设置消息

  • 问题内容: 我正在将struts2用于Action,将jquery用于UI … 我想知道如何将Map对象转换为JSON对象并将其发送回UI, 现在可以在JSP页面中将其打印为普通的Java Map对象: 但我希望它是这样的: 我将如何实现这一目标…? 问题答案: 尝试Gson: 不过,我不建议将这种代码放入JSP。诸如此类的事情应该存在于Servlet或Action类之类的控制器中。 您还绝对不希