Rabbitmq consumes json message and converts to java object

I compiled a java test. It queues the message and returns it as a string. What I'm trying to achieve is to convert it to a SignUpDto java object.

Question:

How do I change the test below to convert to an object?


SignUpClass

public class SignUpDto {
    private String customerName;
    private String isoCountryCode;
    ... etc
}

Application - configuration class

@Configuration
public class Application  {

    @Bean
    public ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory("localhost");
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {

        // updated with @GaryRussels feedback
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }

    @Bean
    public Queue myQueue() {
        return new Queue("myqueue");
    }
}

Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class})
public class TestQueue {

    @Test
    public void convertMessageIntoObject(){

        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        AmqpTemplate template = context.getBean(AmqpTemplate.class);

        String jsonString = "{ \"customerName\": \"TestName\", \"isoCountryCode\": \"UK\" }";

        template.convertAndSend("myqueue", jsonString);

        String foo = (String) template.receiveAndConvert("myqueue");

        // this works ok    
        System.out.println(foo);

        // How do I make this convert
        //SignUpDto objFoo = (SignUpDto) template.receiveAndConvert("myqueue");
        // objFoo.toString()  

    }
}
+4
source share
1 answer

Configure RabbitTemplatewith Jackson2JsonMessageConverter.

Then use

template.convertAndSend("myqueue", myDto);

...

SignUpDto out = (SignUpDto) template.receiveAndConvert("myQueue");

Note that the outbound conversion sets the content type (application / json) and headers with type information, which tells the receiving converter which type of object to create.

JSON, application/json. , ( ), ClassMapper, .

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
         message-converter="json" />

<bean id="json"
 class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

, Java Config; .

EDIT2

JSON; .

...

template.convertAndSend("", "myQueue", jsonString, new MessagePostProcessor() {

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        message.getMessageProperties().setContentType("application/json");
        message.getMessageProperties().getHeaders()
            .put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME, "foo.SignUpDto");
        return message;
    }
});

, JSON ( SimpleMessageConverter). JSON .

+7
source

All Articles