RabbitMQ没有序列化消息,错误转换

时间:2017-09-11 14:16:16

标签: jackson rabbitmq spring-amqp spring-rabbitmq

我在这里看到了一些相关问题,但没有一个对我有用,兔子不会将来自其他应用程序的消息序列化。

Caused by: org.springframework.amqp.AmqpException: No method found for class [B

在我的配置类下面接收消息。

@Configuration
public class RabbitConfiguration implements RabbitListenerConfigurer{

    public final static String EXCHANGE_NAME = "wallet-accounts"; 
    public final static String QUEUE_PAYMENT = "wallet-accounts.payment";
    public final static String QUEUE_RECHARGE = "wallet-accounts.recharge";

    @Bean
    public List<Declarable> ds() {
        return queues(QUEUE_PAYMENT, QUEUE_RECHARGE);
    }

    @Autowired
    private ConnectionFactory rabbitConnectionFactory;

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(rabbitConnectionFactory);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    private List<Declarable> queues(String ... names){
        List<Declarable> result = new ArrayList<>();

        for (int i = 0; i < names.length; i++) {
            result.add(makeQueue(names[i]));
            result.add(makeBinding(names[i]));
        }
        return result;
    }

    private static Binding makeBinding(String queueName){
        return new Binding(queueName, DestinationType.QUEUE, EXCHANGE_NAME, queueName, null);
    }

    private static Queue makeQueue(String name){
        return new Queue(name);
    }

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

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

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

使用其他配置,错误几乎相同:

Caused by: org.springframework.amqp.support.converter.MessageConversionException: failed to resolve class name. Class not found [br.com.beblue.wallet.payment.application.accounts.PaymentEntryCommand]

配置:

@Configuration
public class RabbitConfiguration {

    public final static String EXCHANGE_NAME = "wallet-accounts"; 

    public final static String QUEUE_PAYMENT = "wallet-accounts.payment";
    public final static String QUEUE_RECHARGE = "wallet-accounts.recharge";

    @Bean
    public List<Declarable> ds() {
        return queues(QUEUE_PAYMENT, QUEUE_RECHARGE);
    }

    @Autowired
    private ConnectionFactory rabbitConnectionFactory;

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(rabbitConnectionFactory);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    private List<Declarable> queues(String ... names){
        List<Declarable> result = new ArrayList<>();

        for (int i = 0; i < names.length; i++) {
            result.add(makeQueue(names[i]));
            result.add(makeBinding(names[i]));
        }
        return result;
    }

    private static Binding makeBinding(String queueName){
        return new Binding(queueName, DestinationType.QUEUE, EXCHANGE_NAME, queueName, null);
    }

    private static Queue makeQueue(String name){
        return new Queue(name);
    }
}

有谁能告诉我这些设置有什么问题,或者缺少什么?

1 个答案:

答案 0 :(得分:3)

  

没有找到类[B

的方法

表示存在默认SimpleMessageConverter,无法转换您的传入application/json。它只是没有意识到content-type而只是回到byte[]返回。

  

未找到班级[br.com.beblue.wallet.payment.application.accounts.PaymentEntryCommand]

表示Jackson2JsonMessageConverter无法转换您的application/json,因为无法在本地类路径中找到表示内容类的传入__TypeId__标头。

那么,DefaultMessageHandlerMethodFactory的配置肯定对AMQP转换没有意义。您应该考虑使用SimpleRabbitListenerContainerFactory bean定义及其setMessageConverter。是的,考虑注入适当的org.springframework.amqp.support.converter.MessageConverter实现。

https://docs.spring.io/spring-amqp/docs/1.7.3.RELEASE/reference/html/_reference.html#async-annotation-conversion

从Spring Boot的角度来看,SimpleRabbitListenerContainerFactoryConfigurer就此问题进行了配置:

https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/#boot-features-using-amqp-receiving