[英]Make Kafka ignore object's deserialiser and use Kafka deserialiazer
I have a scenario where I have custom implemented deseralization to my class Item extends JsonDeserializer<User>
which deserializes my object in one fashion.我有一个场景,我自定义实现了对我的 class 的反序列化
Item extends JsonDeserializer<User>
以一种方式反序列化我的 object。
While Kafka uses org.springframework.kafka.support.serializer.JsonDeserializer
which is being overridden by this my new implementation.虽然 Kafka 使用了
org.springframework.kafka.support.serializer.JsonDeserializer
,但我的新实现已经覆盖了它。
How can I make my Kafka stop from using the custom implementation?如何让我的 Kafka 停止使用自定义实现?
Custom Implemented code自定义实现代码
public class ItemDeserializer extends JsonDeserializer<Item> {
public ItemDeserializer() {
this(null);
}
public ItemDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Item deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
int id = (Integer) ((IntNode) node.get("id")).numberValue();
String itemName = node.get("itemName").asText();
int userId = (Integer) ((IntNode) node.get("createdBy")).numberValue();
return new Item(id, itemName, new User(userId, null));
}}
And while using custom deserialiser, I call it like在使用自定义反序列化器时,我称之为
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);
Item readValue = mapper.readValue(json, Item.class);
Item class is annotated with项目 class 注释为
@JsonDeserialize(using = ItemDeserializer.class)
public class Item {
public int id;
public String itemName;
public User owner;
}
Kafka Configurations卡夫卡配置
consumer:
deserializer:
key.delegate.class: com.apache.kafka.common.serialization.stringDeserializer
value.delegate.class: com.apache.kafka.common.serialization.JsonDeserializer
json:
trusted:
packages: com.package.to.item
Tried Searching for 3 days.尝试搜索 3 天。 This is my last resort on how to solve this problem
这是我如何解决这个问题的最后手段
You're creating your own ObjectMapper.您正在创建自己的 ObjectMapper。 If you want to configure Spring-Kafka's ObjectMapper instance, then see here
如果要配置 Spring-Kafka 的 ObjectMapper 实例,请参见此处
make my Kafka stop from using the custom implementation
让我的 Kafka 停止使用自定义实现
If you want to use the String Deserializer rather than JSON, then you don't need delegates .如果你想使用 String Deserializer 而不是 JSON,那么你不需要delegates 。 Simply set the deserializer directly
只需直接设置解串器
spring:
kafka:
consumer:
key-deserializer: com.apache.kafka.common.serialization.StringDeserializer
value-deserializer: ...
Beyond this, unclear why you'd actually want strings that you'd need to deserialize yourself rather than consuming actual Item classes除此之外,不清楚为什么您实际上需要需要反序列化自己而不是使用实际 Item 类的字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.