Victor K.
Victor K.

Reputation: 161

How to parse JSON object in Spring AMQP?

There is a remote RabbitMQ server which allows me only send and receive messages. That means I can't create anything. I send a certain request and get an answer in JSON format.

In my application I have a simple Receiver class:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Receiver {

@RabbitListener(queues = "QueueName")
    public void receive(String in) {
        System.out.println(" [x] Received '" + in + "'");
    }
}

And I expect to receive a JSON object. Instead of this I get a bunch of numbers:

 [x] Received '123,34,97,99,116,105,111,110,34,58,34,103,101,116,73,110,102,111,34,44,34,114,101,115,117,108,116,34,58,34,78,111,116,82,101,103,105,115,116,101,114,101,100,34,44,34,112,97,121,108,111,97,100,34,58,110,117,108,108,125'

I suppose that is the JSON object but how can I covert it to readable format?

Upvotes: 3

Views: 2870

Answers (2)

Victor K.
Victor K.

Reputation: 161

There is one more good answer to my question.

You can simply change the signature of the listener method changing argument from String to org.springframework.amqp.core.Message. It looks like this:

public void receive(Message in) {

The output will be:

[x] Received '(Body:'{"action":"getInfo","result":"NotRegistered","payload":null}' MessageProperties [headers={timestamp_in_ms=1536656426171}, timestamp=Tue Sep 11 16:00:26 KRAT 2018, contentType=application/json, contentLength=0, redelivered=false, receivedExchange=, receivedRoutingKey=routing_key, deliveryTag=1, consumerTag=amq.ctag-gHh9m1_cs52ramBUle_x0A, consumerQueue=queue])'

If you don't want to get all the information about the message then the listener method will be:

@RabbitListener(queues = "queue")
    public void receive(Message in) {
        String body = new String(in.getBody());
        System.out.println(" [x] Received '" + body + "'");
    }

I guess this output fits you better now:

 [x] Received '{"action":"getInfo","result":"NotRegistered","payload":null}'

You can also look through a similiar problem here: Converting Message from RabbitMQ into string/json

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32535

Most probably message is sent in wrong format (stringified array of bytes) thus you are getting such form of message. Either modify sender or convert this to proper string like that .

    String str = "123,34,97,99,116,105,111,110,34,58,34,103,101,116,73,110,102,111,34,44,34,114,101,115,117,108,116,34,58,34,78,111,116,82,101,103,105,115,116,101,114,101,100,34,44,34,112,97,121,108,111,97,100,34,58,110,117,108,108,125";
    String[] chars = str.split(",");
    StringBuilder builder = new StringBuilder();
    Arrays.asList(chars).stream().map(Byte::valueOf).forEach(b -> builder.append((char)b.byteValue()));
    System.out.println(builder.toString());

whitch outputs

{"action":"getInfo","result":"NotRegistered","payload":null}

Upvotes: 5

Related Questions