• RabbitMQ传递序列化/反序列化自定义对象时踩坑


    问题1

    错误信息:

    java.lang.IllegalArgumentException: SimpleMessageConverter only supports String, byte[] and Serializable payloads, received: com.hmall.api.dto.CartRemoveDTO
    
    • 1

    错误信息提示:RabbitMQ默认的消息转换器SimpleMessageConverter 无法对自定义对象生效;
    解决方法:
    让自定义对象实现Serializable接口,并配置消息转换器生产者方和消费者方都要配置,如下所示:

        @Bean
        public MessageConverter messageConverter(){
            return new Jackson2JsonMessageConverter();
        }
    
    • 1
    • 2
    • 3
    • 4

    问题2

    错误信息:

    Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.hmall.api.dto.CartClearDTO` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
     at [Source: (String)"{"userId":1,"itemIds":[1533902]}"; line: 1, column: 2]
    	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1904) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1349) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1415) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:352) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674) ~[jackson-databind-2.13.5.jar:2.13.5]
    	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629) ~[jackson-databind-2.13.5.jar:2.13.5]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    错误提示在jackson方式反序列化时出现了错误,查阅得知,自定义对象只添加了@Data注解,而jackson的反序列化需要无参构造函数,
    解决方法:
    给自定义对象添加@NoArgsConstructor注解或者配置无参构造器。

    参考链接:Jackson反序列化报错

  • 相关阅读:
    全网最全最详细的Sharding-JDBC入门
    Kubernetes 100个常用命令!点赞收藏一键三连
    【C语言-程序编译】一行行代码究竟是怎么一步步到可执行程序的?
    带你从源码看看webpack 的构建流程(上)?
    HTML静态网页成品作业(HTML+CSS+JS)——我的家乡福州介绍网页(3个页面)
    springboot实现自定义注解限流
    Windows系统关机后自动重启的解决方法
    ITSS认证现场的评估过程基本步骤
    【软考】-- 操作系统(中)
    分布式数据库选型之争:数据库向左,中间件向右
  • 原文地址:https://blog.csdn.net/Mr_Richard/article/details/138096124