org.springframework.messaging.converter.MessageConversionException:未找到从实际负载类型“byte[]”到预期负载的转换器

问题描述 投票:0回答:2

我有一个Spring Boot应用程序,在配置类中使用@EnableJMS,在侦听器类中使用@JMSListener

在配置类中,我创建了一个用于连接工厂的bean和一个用于消息转换器的bean

类似这样的:

@Bean // Serialize message content to json using TextMessage
  public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    return converter;
  }

   

我的监听器类如下所示:

@Component
public class Receiver {

  @JmsListener(destination = "abcqueue", containerFactory = "myFactory")
  public void receiveMessage(Message<MyModel> mymodel) {
    System.out.println("Received <");
  }

}

错误信息:

; nested exception is org.springframework.messaging.converter.MessageConversionException: No converter found from actual payload type 'byte[]' to expected payload type 'com.model.MyModel', failedMessage=org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@3e8728
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:118) ~[spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:77) ~[spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:736) ~[spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:696) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:318) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:257) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1189) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1179) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1076) [spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_251]
Caused by: org.springframework.messaging.converter.MessageConversionException: No converter found from actual payload type 'byte[]' to expected payload type MyModel'
    at org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver.convertPayload(MessageMethodArgumentResolver.java:135) ~[spring-messaging-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver.resolveArgument(MessageMethodArgumentResolver.java:93) ~[spring-messaging-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117) ~[spring-messaging-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:148) ~[spring-messaging-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:116) ~[spring-messaging-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:114) ~[spring-jms-5.1.8.RELEASE.jar:5.1.8.RELEASE]
java spring spring-jms
2个回答
0
投票

我有类似的问题,当我的模型中有

LocalDateTime
,但得到
ZonedDateTime
值(2023-01-10T12:00:28.591 + 0000)。

然后我在模型类中将

LocalDateTime
更改为
ZonedDateTime
,问题就消失了。


0
投票

通常当内容类型设置不正确时发生。设置

Content-Type: application/json
为消息然后应该可以工作。

如果您直接从 AWS SQS 发布消息,请按照以下步骤操作:

消息正文字段(文本区域)下,您可能会找到消息属性。将其折叠,然后在

Enter name
字段中输入 contentType,并在
Enter Value
字段中输入 application/json

enter image description here

希望对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.