原因:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到 class org.springframework.core.convert.support.Defa 的序列化程序

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.core.convert.support.Defa

我正在处理 Spring Cloud Stream Apache Kafka 示例。我正在开发代码参考:https://www.youtube.com/watch?v=YPDzcmqwCNo

org.springframework.messaging.MessageDeliveryException: failed to send Message to channel 'pvout'; nested exception is org.springframework.messaging.converter.MessageConversionException: Could not write JSON: No serializer found for class org.springframework.core.convert.support.DefaultConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.cloud.stream.messaging.DirectWithAttributesChannel["conversionService"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.core.convert.support.DefaultConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.cloud.stream.messaging.DirectWithAttributesChannel["conversionService"])
    at org.springframework.integration.support.utils.IntegrationUtils.wrapInDeliveryExceptionIfNecessary(IntegrationUtils.java:166) ~[spring-integration-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:483) ~[spring-integration-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:403) ~[spring-integration-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at com.example.SpringCloudStreamKafkaStreamsJlongApplication$PageViewEventSource.lambda$run[=13=](SpringCloudStreamKafkaStreamsJlongApplication.java:56) ~[classes/:na]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[na:1.8.0_151]
    at java.util.concurrent.FutureTask.runAndReset(Unknown Source) ~[na:1.8.0_151]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(Unknown Source) ~[na:1.8.0_151]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[na:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:1.8.0_151]
    at java.lang.Thread.run(Unknown Source) ~[na:1.8.0_151]
Caused by: org.springframework.messaging.converter.MessageConversionException: Could not write JSON: No serializer found for class org.springframework.core.convert.support.DefaultConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.cloud.stream.messaging.DirectWithAttributesChannel["conversionService"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.core.convert.support.DefaultConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.cloud.stream.messaging.DirectWithAttributesChannel["conversionService"])
    at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertToInternal(MappingJackson2MessageConverter.java:285) ~[spring-messaging-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.cloud.stream.converter.ApplicationJsonMessageMarshallingConverter.convertToInternal(ApplicationJsonMessageMarshallingConverter.java:71) ~[spring-cloud-stream-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.springframework.messaging.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:217) ~[spring-messaging-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.messaging.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:207) ~[spring-messaging-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.messaging.converter.CompositeMessageConverter.toMessage(CompositeMessageConverter.java:83) ~[spring-messaging-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.cloud.stream.binding.MessageConverterConfigurer$OutboundContentTypeConvertingInterceptor.doPreSend(MessageConverterConfigurer.java:263) ~[spring-cloud-stream-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.springframework.cloud.stream.binding.MessageConverterConfigurer$AbstractContentTypeInterceptor.preSend(MessageConverterConfigurer.java:295) ~[spring-cloud-stream-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel$ChannelInterceptorList.preSend(AbstractMessageChannel.java:613) ~[spring-integration-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:443) ~[spring-integration-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    ... 9 common frames omitted
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.core.convert.support.DefaultConversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.cloud.stream.messaging.DirectWithAttributesChannel["conversionService"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:404) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:722) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.10.1.jar:2.10.1]
    at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2795) ~[jackson-databind-2.10.1.jar:2.10.1]
    at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertToInternal(MappingJackson2MessageConverter.java:269) ~[spring-messaging-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    ... 17 common frames omitted

代码:

@Slf4j
@SpringBootApplication
@EnableBinding(AnalyticsBinding.class)
public class SpringCloudStreamKafkaStreamsJlongApplication {

    @Component
    public static class PageViewEventSource implements ApplicationRunner{
        private final MessageChannel pageViewOut;

        public PageViewEventSource(AnalyticsBinding binding) {
            this.pageViewOut = binding.pageViewOut();
        }

        @Override
        public void run(ApplicationArguments args) throws Exception {
            List<String> names = java.util.Arrays.asList("jlong", "sNinawe", "sKarande", "sParate", "nDekate", "kNimje");
            List<String> pages = java.util.Arrays.asList("blog", "sitemap", "initializr", "news", "cricket", "football");

            Runnable runnable = () -> {
                String rPage = pages.get(new Random().nextInt(pages.size()));
                String rName = pages.get(new Random().nextInt(pages.size()));

                PageViewEvent pageViewEvent = new PageViewEvent(rName, rPage, Math.random() > .5 ? 10 : 1000);

                Message<MessageChannel> message = MessageBuilder.withPayload(pageViewOut)
                        .setHeader(KafkaHeaders.MESSAGE_KEY, pageViewEvent.getUserId().getBytes())
                        .build();

                try {
                    this.pageViewOut.send(message);
                    log.info("Sent "+ message.toString());
                } catch (Exception e) {
                    log.error("", e);
                }
            };
            Executors.newScheduledThreadPool(1).scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);
        }
    }

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

}

interface AnalyticsBinding{
    String PAGE_VIEW_OUT = "pvout";

    @Output(PAGE_VIEW_OUT)
    MessageChannel pageViewOut();
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
class PageViewEvent{
    private String userId, page;
    private long duration;
}

application.properties

spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.mms=100
spring.cloud.stream.kafka.streams.binder.configuration.default.key.serde=org.apache.kafka.common.serialization.Serdes$StringSerde
spring.cloud.stream.kafka.streams.binder.configuration.default.value.serde=org.apache.kafka.common.serialization.Serdes$StringSerde

#
# page view out
spring.cloud.stream.bindings.pvout.destination=pvs
spring.cloud.stream.bindings.pvout.producer.header-mode=headers

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-stream-kafka-streams-jlong</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-stream-kafka-streams-jlong</name>
    <description>Spring Boot Open API Spec</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

    <dependencies>
        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-test-support</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

看起来该应用程序有点落后于用于 Spring 启动和 Spring 云的版本。不过,该教程中解释的概念仍然完全有效。我发送了一个 PR 到用于 spring-tips 的原始存储库,我在其中更新了使用的版本。更重要的是,实际代码也进行了升级,以反映Spring Cloud Stream中最新推荐的编写组件的功能模型。我希望这有帮助。