Spring Kafka:JsonDeserializer 没有获取 TRUSTED_PACKAGE 配置
Spring Kafka: JsonDeserializer doesn't pick up TRUSTED_PACKAGE config
我只想检查它是否是已知行为或者我做错了什么。
我使用 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
问题是你实际上没有配置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
设置的情况下工作)
我只想检查它是否是已知行为或者我做错了什么。
我使用 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
问题是你实际上没有配置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
设置的情况下工作)