gpt4 book ai didi

jackson - RabbitMQ 未序列化消息,错误转换

转载 作者:行者123 更新时间:2023-12-05 03:08:04 28 4
gpt4 key购买 nike

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

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

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

最佳答案

No method found for class [B

意味着有一个默认的 SimpleMessageConverter 不能转换你传入的 application/json。它只是不知道 content-type 并且只是回退到 byte[] 以返回。

Class not found [br.com.beblue.wallet.payment.application.accounts.PaymentEntryCommand]

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

当然,您对 DefaultMessageHandlerMethodFactory 的配置对于 AMQP 转换没有意义。您应该考虑使用 SimpleRabbitListenerContainerFactory bean 定义及其 setMessageConverter。是的,考虑注入(inject)正确的 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

关于jackson - RabbitMQ 未序列化消息,错误转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157864/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com