Spring Kafka:JsonDisializer不支持Trusted_Package配置 [英] Spring Kafka: JsonDeserializer doesn't pick up TRUSTED_PACKAGE config

查看:36
本文介绍了Spring Kafka:JsonDisializer不支持Trusted_Package配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想检查这是已知行为还是我做错了什么。

I使用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设置即可运行)

这篇关于Spring Kafka:JsonDisializer不支持Trusted_Package配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆