最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to ignore unknown properties in payload in Spring Cloud Stream Kafka? - Stack Overflow

programmeradmin4浏览0评论

Is there a way to be able to ignore unknown properties of a payload and still deserialize it to the target class when using Spring Cloud Stream binder?

import .springframework.messaging.Message;
....
 @Bean
 Consumer<Message<MyClass>> consumeMessage(Processor processor) {
     return processor::processMessage;
 }

....
public class Processor {

  public void processMessage(Message<MyClass> myMessage) {
     MyClass myClass = myMessage.getPayload();
  }

}

With the default setup, when the payload on Kafka has an extra property, myMessage.getPayload() cannot be cast to MyClass as it is a byte array.

I have tried some customization, such as:

    @Bean
    public MessagingMessageConverter myConverterBean() {
        MessagingMessageConverter converter = new MessagingMessageConverter();
        converter.setMessagingConverter(new MappingJacksonParameterizedConverter());
        return converter;
    }

but the payload is mapped to an Object instead of MyClass and I get java.util.LinkedHashMap cannot be cast so I don't think I am on the right track.

I also need to keep my solution quite generic, to be able to apply it to multiple consumers where payload deserialization should be more relaxed.

Is there any way to achieve this?

发布评论

评论列表(0)

  1. 暂无评论