16 Haziran 2023 Cuma

SpringKafka Consumer JsonSerde Sınıfı

Giriş
Şu satırı dahil ederiz
import  org.springframework.kafka.support.serializer.JsonSerde;
JSON nesnesinden, Java nesnesine çevirir.
Örnek
Şöyle yaparız
public static JsonSerde<StockPrice> stockPrice() {
  JsonSerde<StockPrice> serde = new JsonSerde<>(StockPrice.class);
  serde.configure(Map.of(JsonDeserializer.TRUSTED_PACKAGES, "*"), false);
  return serde;
}
Açıklaması şöyle
For security reasons, configuration is required to specify the trusted packages of the data classes for deserialization.

Here we have the wildcard (*) to trust all for demo purposes. Production code should better have the packages defined accordingly.

Besides, the boolean flag is to let the JsonSerDes knows whether the StockPrice is a message key or not (false means it is a message value in our case).



Hiç yorum yok:

Yorum Gönder