2

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

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?

Thanks a lot!

Sample Input Json message which I will be consuming:

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

1 に答える 1

3

Spring AMQP のどのバージョンを使用していますか?

1.6 以上の場合、フレームワークは引数の型をメッセージ コンバーターに渡します。

1.6 より前では、メッセージ ヘッダーに型情報が必要か、型情報を使用してコンバータを構成する必要があります。

つまり、コンバーターがマップを作成したため、それが (リストではなく) 受信したことを意味します。

JSON のサンプルをメッセージに表示してください。

編集

そのタイプの単一の Bean がある場合、ブートはメッセージコンバーターを自動構成することに注意してください...

@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]]

ここに画像の説明を入力

于 2016-11-08T16:29:10.967 に答える