找不到能够从[byte []]类型转换为[CustomClass]类型的转换器

时间:2018-03-28 10:47:30

标签: spring-boot rabbitmq spring-cloud spring-cloud-stream spring-rabbitmq

我使用spring-boot 1.5.9.RELEASE和spring cloud版本Edgware.SR1将消息从一个应用程序发送到RabbitMQ,然后以这种方式检索消息:

发送讯息:

public interface EventNotificationChannel {
    @Output
    MessageChannel moneyTransferredChannel();
}


@Slf4j
@RestController
public class TransferController {

    private final MessageChannel channel;

    public TransferController(EventNotificationChannel channel) {
        channel = channel.myChannel();
    }

    @PostMapping("/mapping")
    public void do(@RequestBody CustomClass customClass) {
        Message<CustomClass> event =
                 MessageBuilder.withPayload(customClass).build();
        this.channel.send(event);
    }
}

检索邮件:

@Bean
IntegrationFlow integrationFlow(EventNotificationChannel channel) {
        return IntegrationFlows.from(channel.subscriptionOnMyChannel()).
                handle(CustomClass, (payload, headers) -> {
// do something with the payload - payload instanceOf CustomClass
                    return null;
                }).get();
    }

将我的版本升级到

spring-boot 2.0.0.RELEASE
spring-cloud-version Finchley.M9

我正在检索此异常:

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:1506) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1417) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1337) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1324) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1303) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:785) [spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:769) [spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:77) [spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1010) [spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]
Caused by: org.springframework.messaging.MessageHandlingException: error occurred in message handler [ServiceActivator for [org.springframework.integration.handler.LambdaMessageProcessor@af60a61] (org.springframework.integration.handler.ServiceActivatingHandler#0)]; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [byte[]] to type [com.CustomClass]

看起来我必须注册我自己的转换器,因为(我的猜测)堆栈跟踪的相关部分是:

nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [byte[]] to type [com.CustomClass]

有没有办法让这段代码再次运行? 谢谢你的帮助

1 个答案:

答案 0 :(得分:3)

这很奇怪,但看起来这个问题与lambda支持有关,这段代码适用于这种情况:

@Bean
IntegrationFlow integrationFlow(EventNotificationChannel channel) {
        return IntegrationFlows.from(channel.subscriptionOnMyChannel()).
                handle(CustomClass, new GenericHandler<CustomClass>() {
                @Override
                public Object handle(CustomClass payload, 
                                    Map<String, Object> headers) {
                // do something with the payload
                    return null;
                }}).get();
    }