如何在spring-kafka中解决MessageConversionException?

时间:2019-01-27 01:31:30

标签: java apache-kafka type-conversion spring-kafka

我一直在为项目使用spring-kafka,目前,我面临一个问题。

Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.lang.String] to [com.polubentcev.messenger.model.subscription.SubscriptionsRequest] for GenericMessage [payload={"userId":"4","pagination":{"offset":0,"quantity":10}}, headers={kafka_offset=23, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@3817b042, kafka_correlationId=[B@13cf505b, kafka_timestampType=CREATE_TIME, kafka_replyTopic=[B@58355ff, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=07, kafka_receivedTimestamp=1548552030765, __TypeId__=[B@7480d2ef}]
    at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:144) ~[spring-messaging-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117) ~[spring-messaging-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:147) ~[spring-messaging-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:116) ~[spring-messaging-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48) ~[spring-kafka-2.2.3.RELEASE.jar:2.2.3.RELEASE]
    at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:283) ~[spring-kafka-2.2.3.RELEASE.jar:2.2.3.RELEASE]
    ... 14 common frames omitted

当消息到达我的微服务时会引发这种异常。

这是我的配置:

private final Map<String, Object> consumerProps;

public KafkaConsumerConfiguration(@Value("${kafka.bootstrap-servers}") final String bootstrapServers,
                                  @Value("${kafka.consumer.group-id}") final String consumerGroupId) {
    this.consumerProps = Map.of(
            ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers,
            ConsumerConfig.GROUP_ID_CONFIG, consumerGroupId,
            ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringSerializer.class,
            ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonSerializer.class
    );
}

@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, Pair<String, String>>>
kafkaSubscriptionListenerContainerFactory(final ReplyingKafkaTemplate<String, Pair<String, String>, UsersSubscription> kafkaTemplate) {
    final DefaultKafkaConsumerFactory<String, Pair<String, String>> cf = new DefaultKafkaConsumerFactory<>(
            consumerProps, new StringDeserializer(), new JsonDeserializer<>()
    );
    final ConcurrentKafkaListenerContainerFactory<String, Pair<String, String>> factory =
            new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(cf);
    factory.setReplyTemplate(kafkaTemplate);
    return factory;
}

这很奇怪,因为它必须工作。我试图自己解决该问题,但由于不知道在哪里进行深入研究而未能成功。

您能帮我吗?

顺便说一句,我尝试在调试器中使用新的String转换有效负载ObjectMapper<>().readValue(),并且有效,所有SubscriptionsRequest都包含@JsonCreator以及用于反序列化的所有必要内容。 / p>

解决方法:我添加了一个SubscriptionsRequest(String str)构造函数,在其中我使用ObjectMapper构建了一个实例。可以,但是很丑。

0 个答案:

没有答案