如何将多种类型的 类 发送到 Spring 流源

How to send multiple types of classes into Spring stream source

我将 spring 流从 1.3.0 升级到 2.1.2,默认序列化程序从 Kyro(已弃用)更改为 Jackson。

我有一个 kafka 主题,可以向其发送不止一种类型的消息。使用 Kyro,我曾经将其反序列化为 Object.class,然后将其转换为 class.

的相关类型

使用jackson我无法实现这个功能,因为我必须提前指定class我要反序列化到的类型,否则,它被反序列化成一个字符串。

我试图找到一个例子,但找不到任何东西。任何想法如何实现相同的功能?我想让它尽可能高效。

如果需要,您仍然可以使用 Kryo。您可以使用 @StreamMessageConverter- https://cloud.spring.io/spring-cloud-stream/spring-cloud-stream.html#spring-cloud-stream-overview-user-defined-message-converters.

手动添加它

关于 "With jackson I can't achieve this functionality, because I have to specify the type of class. . ." - 这是不准确的,因为 class 的类型是从处理程序方法的签名中获取的,并且它对您作为用户是透明的。

您可以向 Jackson 编码添加提示,以便将其解码为正确的具体类型:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")

@SpringBootApplication
@EnableBinding(Processor.class)
public class So56753956Application {

    public static void main(String[] args) {
        SpringApplication.run(So56753956Application.class, args);
    }

    @StreamListener(Processor.INPUT)
    public void listen(Foo foo) {
        System.out.println(foo);
    }


    @Bean
    public ApplicationRunner runner(MessageChannel output) {
        return args -> {
            output.send(new GenericMessage<>(new Bar("fiz")));
            output.send(new GenericMessage<>(new Baz("buz")));
        };
    }

    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
    public static abstract class Foo {

        private String bar;

        public Foo() {
            super();
        }

        public Foo(String bar) {
            this.bar = bar;
        }

        public String getBar() {
            return this.bar;
        }

        public void setBar(String bar) {
            this.bar = bar;
        }

        @Override
        public String toString() {
            return getClass().getName() + " [bar=" + this.bar + "]";
        }

    }

    public static class Bar extends Foo {

        public Bar() {
            super();
        }

        public Bar(String bar) {
            super(bar);
        }

    }

    public static class Baz extends Foo {

        public Baz() {
            super();
        }

        public Baz(String bar) {
            super(bar);
        }

    }

}

com.example.So56753956Application$Bar [bar=fiz]
com.example.So56753956Application$Baz [bar=buz]

here