rabbitmq使用json消息并转换为Java对象

Rob*_*_UK 6 java rabbitmq spring-amqp spring-rabbitmq

我已经整理了一个java测试.它将消息放入队列并将其作为字符串返回.我想要实现的是它转换为java对象SignUpDto.我已经尽可能地删除了代码.

问题:

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


SignUpClass

public class SignUpDto {
    private String customerName;
    private String isoCountryCode;
    ... etc
}
Run Code Online (Sandbox Code Playgroud)

应用程序 - 配置类

@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");
    }
}
Run Code Online (Sandbox Code Playgroud)

考试

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

    }
}
Run Code Online (Sandbox Code Playgroud)

Gar*_*ell 8

配置RabbitTemplatea Jackson2JsonMessageConverter.

然后用

template.convertAndSend("myqueue", myDto);

...

SignUpDto out = (SignUpDto) template.receiveAndConvert("myQueue");
Run Code Online (Sandbox Code Playgroud)

请注意,出站转换使用类型信息设置内容类型(application/json)和标头,告知接收转换器要创建的对象类型.

如果您确实想要发送简单的JSON字符串,则需要将内容类型设置为application/json.为了帮助进行入站转换,您可以设置类型标题(查看转换器源以获取信息),也可以使用a配置转换器ClassMapper以确定类型.

编辑

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

<bean id="json"
 class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
Run Code Online (Sandbox Code Playgroud)

或者,因为您使用的是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;
    }
});
Run Code Online (Sandbox Code Playgroud)

但请记住,此发送模板必须没有 JSON消息转换器(让它默认为SimpleMessageConverter).否则,JSON将被双重编码.

  • 在你的`RabbitTemplate`` @ Bean`定义中:`template.setMessageConverter(new Jackson2JsonMessageConverter());`. (2认同)