使用RabbitMQ消费JSON消息并转换为Java对象

7
我已经编写了一个Java测试。它将一条消息放入队列并将其作为字符串返回。我的目标是将其转换为Java对象SignUpDto。我已经尽可能地精简了问题的代码。
问题是:如何修改下面的测试以将其转换为对象?
SignUpClass
public class SignUpDto {
    private String customerName;
    private String isoCountryCode;
    ... etc
}

应用程序 - 配置类
@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()  

    }
}
1个回答

11
< p >使用 Jackson2JsonMessageConverter 配置 RabbitTemplate

然后使用:

template.convertAndSend("myqueue", myDto);

...

SignUpDto out = (SignUpDto) template.receiveAndConvert("myQueue");
请注意,出站转换设置内容类型(application/json)和带有类型信息的标头,告诉接收转换器要创建什么对象类型。
如果您确实想发送一个简单的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,则只需将一个注入到模板定义中。

编辑2

如果您想发送一个纯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将被双重编码。


如何使用Jackson2JsonMessageConverter配置RabbitTemplate? - Robbo_UK
我不确定那个 XML 如何映射到 Java 配置? - Robbo_UK
2
在您的RabbitTemplate @Bean定义中:template.setMessageConverter(new Jackson2JsonMessageConverter()); - Gary Russell
现在它可以工作了,谢谢你的帮助 :-) 我已经更新了我的配置设置,使其更易读。最后一个问题是如何设置类型头。 - Robbo_UK
请查看第二次编辑 - 但是请注意结尾的警告。 - Gary Russell
@Gary 感谢您的精彩回答。 - Ankur Mahajan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接