org.springframework.messaging.converter.MessageConversionException:Could not read JSON: Cannot const

使用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;
    }
}

重启项目运行,发送与接收都是正常的了

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值