作者: | 来源:互联网 | 2023-08-25 18:06
使用rabbitMQ出现下面的异常:rg.springframework.messaging.converter.MessageConversionExceptio
使用rabbitMQ出现下面的异常:
rg.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `com.example.mqtest.mq.User` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)at [Source: (byte[])"{"name":"哈哈"}"; line: 1, column: 2]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.example.mqtest.mq.User` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)at [Source: (byte[])"{"name":"哈哈"}"; line: 1, column: 2], failedMessage=GenericMessage [payload=byte[17], headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedExchange=comment, amqp_deliveryTag=1, deliveryAttempt=3, amqp_cOnsumerQueue=comment.comment_like_group111, amqp_redelivered=false, amqp_receivedRoutingKey=comment_like, amqp_timestamp=Fri Mar 27 13:36:57 CST 2020, amqp_messageId=c04aa17c-ae7b-2ca2-1ed1-2d623cc45409, id=3e6f1fc8-00c1-3e93-d6e6-c09c83cf31dc, amqp_cOnsumerTag=amq.ctag-22vW4PCiDJzGNiE5QGjfwQ, sourceData=(Body:'{"name":"哈哈"}' MessageProperties [headers={}, timestamp=Fri Mar 27 13:36:57 CST 2020, messageId=c04aa17c-ae7b-2ca2-1ed1-2d623cc45409, cOntentType=application/json, cOntentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=comment, receivedRoutingKey=comment_like, deliveryTag=1, cOnsumerTag=amq.ctag-22vW4PCiDJzGNiE5QGjfwQ, cOnsumerQueue=comment.comment_like_group111]), cOntentType=application/json, timestamp=1585287417722}]at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:235)at org.springframework.cloud.stream.converter.ApplicationJsonMessageMarshallingConverter.convertFromInternal(ApplicationJsonMessageMarshallingConverter.java:112)at org.springframework.messaging.converter.AbstractMessageConverter.fromMessage(AbstractMessageConverter.java:197)at org.springframework.messaging.converter.CompositeMessageConverter.fromMessage(CompositeMessageConverter.java:70)at org.springframework.cloud.stream.config.SmartPayloadArgumentResolver.resolveArgument(SmartPayloadArgumentResolver.java:115)at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117)
在使用springboot stream rabbitmq时候 新建了项目,配置好后发送对象消息到mq
然后报了如上错误,进行代码跟踪了下。发现无法解析User类,因为user类使用的lombok注解,模型如下
import lombok.Data;import java.io.Serializable;@Data
public class User implements Serializable {private String name;public User(String name) {this.name = name;}
}
因为给了有属性的构造函数,没有指定出空的构造,所以mq处理的方法拿到class处理没法拿到对象的句柄地址,也没法进行序列化操作
,解决办法是加了个默认的空构造进去,当然你没有有参构造是不用加空默认的
import lombok.Data;import java.io.Serializable;@Data
public class User implements Serializable {private String name;public User() {}public User(String name) {this.name = name;}
}
重启项目运行,发送与接收都是正常的了