java 将消息从 RabbitMQ 转换为 string/json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12748284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:03:20  来源:igfitidea点击:

Converting Message from RabbitMQ into string/json

javajsonspringrabbitmqamqp

提问by MatthiasLaug

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.

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

The Messageobject 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)

The configuration class (using spring)

配置类(使用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();
    }

}

and the message listener

和消息监听器

public class PageQueueListener implements MessageListener {

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

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

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

I feel stupid, but I cannot find a solution here

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

回答by slim

message.getBody()returns a byte[]

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

Try:

尝试:

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

回答by Eranjene S

If you want to parse to a JSONObject, the best way is add the RabbitMQ message to a StringBuilder in String format. Then parse the StringBuilder into a JSONObject, by using any of the conversion utils.
For e.g.:

如果要解析为 JSONObject,最好的方法是将 RabbitMQ 消息以 String 格式添加到 StringBuilder 中。然后使用任何转换工具将 StringBuilder 解析为 JSONObject。
例如:

StringBuilder sb = new StringBuilder();
sb.append(publisher.toString());
payload = (JSONObject)jsonParser.parse(sb.toString());