rabbitmq 消耗 json 消息并转换为 Java 对象

rabbitmq consume json message and convert into Java object

我整理了一个 java 测试。它将一条消息放在队列中,并将其作为字符串 returns。我试图实现的是将其转换为 java 对象 SignUpDto。对于这个问题,我已经尽可能地精简了代码。

问题:

如何修改下面的测试以转换为对象?


SignUpClass

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

应用程序 - 配置 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");
    }
}

测试

@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()  

    }
}

RabbitTemplate 配置为 Jackson2JsonMessageConverter

然后使用

template.convertAndSend("myqueue", myDto);

...

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

请注意,出站转换使用类型信息设置内容类型 (application/json) 和 headers,告诉接收转换器要创建什么 object 类型。

如果您确实想发送一个简单的 JSON 字符串,您需要将内容类型设置为 application/json。为了帮助入站转换,您可以设置类型 headers(查看转换器源以获取信息),或者您可以使用 ClassMapper 配置转换器以确定类型。

编辑

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

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

或者,由于您正在使用 Java 配置;只需将一个注入到您的模板定义中即可。

EDIT2

如果你想发送一个普通的 JSON 字符串;您需要通过 headers.

帮助入站转换器

设置 headers...

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;
    }
});

但请记住,此 发送 模板必须 NOT 具有 JSON 消息转换器(让它默认SimpleMessageConverter)。否则,JSON 将是 double-encoded。