首页 > 解决方案 > 自定义 AvroSchemaMessageConverter 未注册

问题描述

我有一个@SpringBootApplication定义了以下bean的类:

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public MessageConverter avroSchemaMessageConverter() {
  return new AvroSchemaMessageConverter(MimeType.valueOf("avro/bytes"));
}

当我运行 a@SpringBootTest并对其进行调试时,我注意到在我对CompositeMessageConverterFactory. 由于自定义转换器列表作为参数传递给此类,因此我的自定义 AvroSchemaMessageConverter 从未注册。

@Output当我稍后在测试中尝试将 Avro 消息发送到我的频道时,这种情况会导致以下错误:

Caused by: java.lang.IllegalStateException: Failed to convert message: 'GenericMessage [payload={omitted for privacy reasons}, headers={id=omitted, contentType=avro/bytes, __TypeId__=com.example.ExampleRecord, timestamp=1576877638777}]' to outbound message.
    at org.springframework.cloud.stream.binding.MessageConverterConfigurer$OutboundContentTypeConvertingInterceptor.doPreSend(MessageConverterConfigurer.java:389)
    at org.springframework.cloud.stream.binding.MessageConverterConfigurer$AbstractContentTypeInterceptor.preSend(MessageConverterConfigurer.java:423)
    at org.springframework.integration.channel.AbstractMessageChannel$ChannelInterceptorList.preSend(AbstractMessageChannel.java:608)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:443)
    ... 75 common frames omitted

如何确保在CompositeMessageConverterFactory使用其转换器列表初始化之前将我的 bean 添加到 Spring 的应用程序上下文中?

标签: spring-cloud-stream

解决方案


添加@StreamMessageConverter 作为Oleg Zhurakousky 建议为我解决了这个问题。


推荐阅读