Jackson配置使用RabbitMQ中的记录列表 [英] Jackson configuration to consume list of records in rabbitmq

查看:157
本文介绍了Jackson配置使用RabbitMQ中的记录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring boot amqp,其中将使用队列中的Employee对象列表.我的侦听器方法如下:

I am using spring boot amqp in which I will be consuming a list of Employee objects from a queue. My listener method looks like this:

@RabbitListener(queues = "emp_queue")
public void processAndPortEmployeeData(List<Employee> empList) {
    empList.forEach(emp -> { some logic })
}

但是,当我尝试使用该消息时,我得到了一个类强制转换异常:由于某种原因,我得到了LinkedHashMap.

However, when I try to consume the message, I get a class cast exception: For some reason, I'm getting a LinkedHashMap.

Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.integration.domain.Employee

如果我将侦听器方法更改为使用单个员工对象,则它可以正常工作,并且我正在使用以下杰克逊配置:

If I change my listener method to consume a single employee object, it works fine and I'm using the following jackson configurations for it:

@Configuration
@EnableRabbit
public class RabbitConfiguration implements RabbitListenerConfigurer {

@Bean
public MappingJackson2MessageConverter jackson2Converter() {
    return new MappingJackson2MessageConverter();
}

@Bean
public DefaultMessageHandlerMethodFactory handlerMethodFactory() {
    DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
    factory.setMessageConverter(jackson2Converter());
    return factory;
}

@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
    registrar.setMessageHandlerMethodFactory(handlerMethodFactory());
}

}

要使用员工对象列表还需要做其他一些杰克逊配置吗?

Is there some other jackson configuration that I need to do to consume the list of employee objects?

非常感谢!

我将使用的样本输入Json消息:

Sample Input Json message which I will be consuming:

[
  {
    "name" : "Jasmine",
    "age" : "24",
    "emp_id" : 1344
  },
  {
    "name" : "Mark",
    "age" : "32",
    "emp_id" : 1314
  }
]

推荐答案

您正在使用哪个版本的Spring AMQP?

What version of Spring AMQP are you using?

如果大于等于1.6,则框架将参数类型传递给消息转换器.

If 1.6 or greater, the framework passes the argument type to the message converter.

在1.6之前,您要么在消息头中需要类型信息,要么需要在转换器中配置类型信息.

Before 1.6 you either need type information in the message headers, or you need to configure the converter with type information.

也就是说,由于转换器创建了一个映射,所以它暗示的是接收到的内容(而不是列表).

That said, since the converter created a map, it implies that was what received (not a list).

请在消息中显示JSON示例.

Please show a sample of the JSON in a message.

编辑

请注意,如果有单个类型的bean,则启动会自动配置消息转换器...

Note that boot auto-configures the message converter if there's a single bean of that type...

@SpringBootApplication
public class So40491628Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So40491628Application.class, args);
        Resource resource = new ClassPathResource("data.json");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileCopyUtils.copy(resource.getInputStream(), baos);
        context.getBean(RabbitTemplate.class).send("foo", MessageBuilder.withBody(baos.toByteArray())
            .andProperties(MessagePropertiesBuilder.newInstance().setContentType("application/json").build()).build());
        Thread.sleep(10000);
        context.close();
    }

    @Bean
    public Jackson2JsonMessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

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

    @RabbitListener(queues = "foo")
    public void listen(List<Employee> emps) {
        System.out.println(emps);
    }

    public static class Employee {

        private String name;

        private String age;

        private int empId;

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return this.age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public int getEmpId() {
            return this.empId;
        }

        public void setEmpId(int empId) {
            this.empId = empId;
        }

        @Override
        public String toString() {
            return "Employee [name=" + this.name + ", age=" + this.age + ", empId=" + this.empId + "]";
        }

    }
}

结果:

[Employee [name=Jasmine, age=24, empId=0], Employee [name=Mark, age=32, empId=0]]

这篇关于Jackson配置使用RabbitMQ中的记录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆