从RabbitMQ 2.3.6中的消息队列侦听消息时,数据类型发生更改

从RabbitMQ 2.3.6中的消息队列侦听消息时,数据类型发生更改,rabbitmq,spring-rabbit,Rabbitmq,Spring Rabbit,我将SpringRabbit2.3.6用于我的Spring应用程序。我有注释实体列表,并通过此函数发送到消息队列 public void deletePost(long postId){ Post post = postRepository.findById(postId) .orElseThrow(() -> new PostNotFoundException(String.valueOf(postId))); List<Comment>

我将SpringRabbit2.3.6用于我的Spring应用程序。我有注释实体列表,并通过此函数发送到消息队列

public void deletePost(long postId){
    Post post = postRepository.findById(postId)
            .orElseThrow(() -> new PostNotFoundException(String.valueOf(postId)));
    List<Comment> comments = postBase.getComments(post);
    postRepository.delete(post);

    //post event
    Map<String, Object> inputs= new HashMap<>();
    inputs.put(InputParam.OBJECT_ID, postId);
    inputs.put(InputParam.COMMENTS, comments);
    messagingUtil.postEvent(SnwObjectType.POST, SnwActionType.DELETE, inputs);
}

说您的数据类型正在“更改”有点用词不当。这样的类型只存在于Java程序中,RabbitMQ不是程序的一部分。RabbitMQ以与语言无关的格式保存消息。然后,Java程序通过将每个MQ消息的数据绑定到Java对象来对其进行反序列化。因为您的使用者方法将
Map
作为其参数,所以它绑定到的是Java类型。如果您将其替换为一个自定义类,该类
实现可序列化
,并且具有
objectId
注释
字段,这些字段的类型与您期望的类型相同,则可能会解决您的问题。@Noal谢谢you@GaryRussell我更新了消息,我的评论是错误的;删除;我没有注意到您正在发送/接收
Map
。无法推断容器类型的元素类型,例如
Map
。与其使用
映射
,不如创建一个包含所需字段和类型的POJO,然后jackson将使用正确的类型正确解码每个字段。
@RabbitListener(queues = MessagingConfig.QUEUE)
public void consumeMessageFromQueue(Map<String, Object> inputs) {
        
}
@Configuration
public class MessagingConfig {
    public static final String QUEUE = "javatechie_queue";
    public static final String EXCHANGE = "javatechie_exchange";
    public static final String ROUTING_KEY = "javatechie_routingKey";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

    @Bean
    public MessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate template(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(converter());
        return rabbitTemplate;
    }
}