【发布时间】:2020-06-01 20:58:16
【问题描述】:
我正在尝试使用 Spring Cloud Stream 创建一个 Kafka 流应用程序,但我正在努力反序列化输入消息,其值已使用 MessagePack 编码。
这是我目前所得到的:
// TransactionApplication.java
@SpringBootApplication
public class TransactionApplication {
public static void main(String[] args) {
SpringApplication.run(TransactionApplication.class, args);
}
public static class TransactionConsumer {
@Bean
public Serde<Transaction> transactionSerde() {
ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
return new JsonSerde<Transaction>(mapper);
}
@Bean
public Consumer<KStream<String, Transaction>> process() {
return input -> input.foreach((key, value) -> {
System.out.println("Key: " + key + " Value: " + value);
});
}
}
}
// Transaction.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Transaction {
String item;
Number amount;
}
我收到了错误:
java.lang.IllegalStateException:标头中没有类型信息,也没有提供默认类型。
我的 application.yml 是:
spring.cloud.stream:
bindings:
process-in-0:
destination: transactions
kafka:
streams:
binder:
applicationId: transactions-application
configuration:
commit.interval.ms: 100
在我的 applicaton.yml 的 configuration 节点下包含 spring.json.value.default.type: com.example.Transaction 后,我又遇到了另一个错误。见下文。
Caused by: org.apache.kafka.common.errors.SerializationException: Can't deserialize data [[123, 34, 105, 116, 101, 109, 34, 58, 32, 34, 112, 114, 105, 118, 97, 116, 101, 32, 106, 101, 116, 34, 44, 32, 34, 97, 109, 111, 117, 110, 116, 34, 58, 32, 53, 48, 50, 125]] from topic [transactions]
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.jackdry.processors.json.Transaction` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (123)
at [Source: (byte[])"{"item": "private jet", "amount": 502}"; line: -1, column: 0]
【问题讨论】:
-
applicaton.yml是这里的拼写错误,还是在您的系统中也有错误的名称?
标签: java apache-kafka apache-kafka-streams spring-kafka spring-cloud-stream