将消息从 RabbitMQ 转换为字符串/json

Converting Message from RabbitMQ into string/json(将消息从 RabbitMQ 转换为字符串/json)
本文介绍了将消息从 RabbitMQ 转换为字符串/json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在努力解决一个相当简单的问题.我想从 RabbitMQ 接收消息并将其转换为字符串(或稍后转换为 json 对象).但我得到的只是字节.

I am currently struggling hard with a fair simple problem. I want to receive a message from RabbitMQ and have that transformed into a string (or later a json object). But all I get is bytes.

Message 对象以这种方式将自身显示为字符串

The Message object displays itself as a string that way

(Body:'{"cityId":644}'; ID:null; Content:application/json; Headers:{}; Exchange:; RoutingKey:pages.type.index; Reply:null; DeliveryMode:NON_PERSISTENT; DeliveryTag:1)

配置类(使用spring)

The configuration class (using spring)

@Configuration
public class RabbitConfiguration {

    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("www.example.com");
        connectionFactory.setUsername("xxxx");
        connectionFactory.setPassword("xxxx");
        return connectionFactory;
    }

    @Bean
    public MessageConverter jsonMessageConverter(){
        JsonMessageConverter jsonMessageConverter = new JsonMessageConverter();
        return jsonMessageConverter;
    }

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer(){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
        container.setAutoStartup(false);
        container.setQueues(indexQueue());
        container.setConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        container.setMessageListener(new MessageListenerAdapter(pageListener(), jsonMessageConverter()));
        return container;
    }

    @Bean
    public Queue indexQueue(){
        return new Queue("pages.type.index");
    }

    @Bean
    public MessageListener pageListener(){
        return new PageQueueListener();
    }

}

和消息监听器

public class PageQueueListener implements MessageListener {

    public void onMessage(Message message) {
        System.out.println(message);
        System.out.println(message.getBody());
    }
 }

我的问题是,getBody() 方法显示 [B@4dbb73b0 所以没有任何东西被转换.既不是字符串也不是 json 对象:(

my problem is, that the getBody() method displayes [B@4dbb73b0 so nothing is ever converted. Neither to a string nor to a json object :(

我觉得自己很愚蠢,但我在这里找不到解决方案

I feel stupid, but I cannot find a solution here

推荐答案

message.getBody() 返回一个byte[]

试试:

byte[] body = message.getBody();
System.out.println(new String(body));

这篇关于将消息从 RabbitMQ 转换为字符串/json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)