我只想看看这是已知的行为还是我做错了什么。
我使用JsonDeserializer配置生产者和使用者的自定义类型映射。
消费者不能
org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition ticket-1 at offset 1. If needed, please seek past the record to continue consumption.
Caused by: java.lang.IllegalArgumentException: The class 'createTicket' is not in the trusted packages: [java.util, java.lang]. If you believe this class is safe to deserialize, please provide its name. If the serialization is only done by a trusted source, you can also enable trust all (*).消费工厂配置
props.put(JsonDeserializer.TRUSTED_PACKAGES, "*");
props.put(JsonDeserializer.TYPE_MAPPINGS, "createTicket:com.example.application.domain.command.CreateTicket, createTicketCommand:com.example.application.domain.command.CreateTicketCommand");生产者工厂配置
props.put(JsonSerializer.TYPE_MAPPINGS,
"createTicket:com.example.application.domain.command.CreateTicket, createTicketCommand:com.example.application.domain.command.CreateTicketCommand");我用稳定版本和M3版本进行了测试。完全可运行的示例https://github.com/gAmUssA/spring-kafka-question-from-chat
发布于 2020-03-07 23:30:23
问题是您实际上没有配置JsonDeserializer。
JsonDeserializer.TYPE_MAPPINGS将直接传递给JsonDeserializer,而不是传递给ConsumerFactory。您的代码应该看起来像
JsonDeserializer<Object> jsonDeserializer = new JsonDeserializer<>();
Map<String, Object> deserProps = new HashMap<>();
deserProps.put(JsonDeserializer.TYPE_MAPPINGS,
"createTicket:com.example.application.domain.command.CreateTicket, createTicketCommand:com.example.application.domain.command.CreateTicketCommand");
//mind this `false` -- they have different modes for key and value deserializers
jsonDeserializer.configure(deserProps, false);
return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(),
jsonDeserializer);(在我的机器上,它工作时没有任何TRUSTED_PACKAGES设置)
https://stackoverflow.com/questions/60582666
复制相似问题