当前位置:   article > 正文

RabbitMQ(十二)Cannot deserialize value of type `java.lang.String` from Object value 报错整理

cannot deserialize value of type `java.lang.string` from object value (token

消息接收类型错误

1.核心报错内容:

Cannot deserialize value of type java.lang.String from Object value (token JsonToken.START_OBJECT)

2.完整报错内容:

org.springframework.amqp.rabbit.listener.exception.FatalListenerExecutionException: Illegal null id in message. Failed to manage retry for message: (Body:'[B@7f8bf95d(byte[73])' MessageProperties [headers={spring_listener_return_correlation=459805f9-bf6a-4a2b-a060-404b38590dc4, x-first-death-exchange=ANYSIGN_DIRECT_EXCHANGE, x-death=[{reason=rejected, count=1, exchange=ANYSIGN_DIRECT_EXCHANGE, time=Tue Jan 09 15:16:37 CST 2024, routing-keys=[MY_ROUTING_KEY], queue=MY_QUEUE}], x-first-death-reason=rejected, x-first-death-queue=MY_QUEUE, __TypeId__=com.demo.config.mq.dto.MyMessageVO}, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=true, receivedExchange=ANYSIGN_DEAD_LETTER_EXCHANGE, receivedRoutingKey=DEAD_LETTER_ROUTING_KEY, deliveryTag=1, consumerTag=amq.ctag-DBuoqXo7Kr0tIR-3qCCEow, consumerQueue=DEAD_LETTER_QUEUE])
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.checkStatefulRetry(AbstractMessageListenerContainer.java:1590)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1581)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:994)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:941)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:86)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1323)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1225)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:157)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1719)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1638)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.retry.interceptor.StatefulRetryOperationsInterceptor$StatefulMethodInvocationRetryCallback.doWithRetry(StatefulRetryOperationsInterceptor.java:209)
	at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
	at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:255)
	at org.springframework.retry.interceptor.StatefulRetryOperationsInterceptor.invoke(StatefulRetryOperationsInterceptor.java:166)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:241)
	at org.springframework.amqp.rabbit.listener.$Proxy476.invokeListener(Unknown Source)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1626)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1617)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1561)
	... 6 common frames omitted
Caused by: org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content
	at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.doFromMessage(AbstractJackson2MessageConverter.java:367)
	at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.fromMessage(AbstractJackson2MessageConverter.java:321)
	at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.fromMessage(AbstractJackson2MessageConverter.java:304)
	at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:343)
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter$MessagingMessageConverterAdapter.extractPayload(MessagingMessageListenerAdapter.java:367)
	at org.springframework.amqp.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:132)
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:244)
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:147)
	... 25 common frames omitted
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{"contractId":1744614807082086402,"contractPlayerId":1744614859779321858}"; line: 1, column: 1]
	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1420)
	at com.fasterxml.jackson.databind.DeserializationContext.extractScalarFromObject(DeserializationContext.java:932)
	at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
	at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
	at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.convertBytesToObject(AbstractJackson2MessageConverter.java:428)
	at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.convertContent(AbstractJackson2MessageConverter.java:395)
	at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.doFromMessage(AbstractJackson2MessageConverter.java:364)
	... 32 common frames omitted
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

3.报错原因:

消费端代码如下:

@RabbitListener(queues = RabbitMQConfig.DEAD_LETTER_QUEUE_NAME)
public void deadLetter(String body, Message message, Channel channel) throws IOException {
    log.info("<===MQ===【消费者】MQ消费消息,消息内容:{}", body);
}
  • 1
  • 2
  • 3
  • 4

这是因为在使用了 Jackson2JsonMessageConverter 类进行统一JSON序列化之后,还使用 String 类型来接受消息导致的。

4.解决方案:

正常来说,已经通过 Jackson2JsonMessageConverter 转换类统一JSON序列化之后,直接使用原本的 Java 实体类来接收消息即可:

@RabbitListener(queues = RabbitMQConfig.DEAD_LETTER_QUEUE_NAME)
public void deadLetter(User user, Message message, Channel channel) throws IOException {
    log.info("<===MQ===【消费者】MQ消费消息,消息内容:{}", user);
}
  • 1
  • 2
  • 3
  • 4

如果非要使用 String 字符串来进行接收,比如:死信队列中对异常处理的消息统一进行记录,可以使用以下方法手动从 Message 中获取 body 信息。

@RabbitListener(queues = RabbitMQConfig.DEAD_LETTER_QUEUE_NAME)
public void deadLetter(Message message, Channel channel) throws IOException {
    String body = new String(message.getBody());
    log.info("<===MQ===【消费者】MQ消费消息,消息内容:{}", body);
}
  • 1
  • 2
  • 3
  • 4
  • 5

整理完毕,完结撒花~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/170296
推荐阅读
相关标签