1

I'm using Spring Kafka integration and I've my own value generic serializer/deserializer as shown below

Serializer:

public class KafkaSerializer<T> implements Serializer<T> {

  private ObjectMapper mapper;

  @Override
  public void close() {
  }

  @Override
  public void configure(final Map<String, ?> settings, final boolean isKey) {
    mapper = new ObjectMapper();
  }

  @Override
  public byte[] serialize(final String topic, final T object) {
    try {
      return mapper.writeValueAsBytes(object);
    } catch (final JsonProcessingException e) {
      throw new IllegalArgumentException(e);
    }
  }
}

Deserializer:

public class KafkaDeserializer<T> implements Deserializer<T> {

  private ObjectMapper mapper;

  @Override
  public void close() {
  }

  @Override
  public void configure(final Map<String, ?> settings, final boolean isKey) {
    mapper = new ObjectMapper();
  }

  @Override
  public T deserialize(final String topic, final byte[] bytes) {
    try {
      return mapper.readValue(bytes, new TypeReference<T>() {
      });
    } catch (final IOException e) {
      throw new IllegalArgumentException(e);
    }
  }
}

The serializer is working perfectly but when it comes to deserialization of values while consuming message I get a LinkedHashMap instead of desired object, please enlighten me where I'm mistaking, thanks in advance.

Chin Huang
  • 12,912
  • 4
  • 46
  • 47
Akhil
  • 1,184
  • 1
  • 18
  • 42
  • Spring Kafka already provides the [JsonDeserializer](https://github.com/spring-projects/spring-kafka/blob/master/spring-kafka/src/main/java/org/springframework/kafka/support/serializer/JsonDeserializer.java) class to convert JSON to a Java object. – Chin Huang Apr 21 '17 at 13:27
  • 2
    @ChinHuang, thanks for replying, but I need to have a generic consumer and I guess JsonDeserializer constructor take the class type in which the response should be parsed, so how do I leverage it. – Akhil Apr 21 '17 at 15:40
  • @Apollo - how did you solve this problem of generic consumer? – Mubin Nov 21 '17 at 08:32
  • @Mubin, actually I followed very naive approach, I used StringDeserializer to obtain message as string from topic and then converted to required object, however, you can consider Avro for same (which I came across later on) – Akhil Nov 23 '17 at 09:56
  • Thanks for the information @Apollo – Mubin Nov 23 '17 at 09:59

1 Answers1

0

Some situations need be confirmed:

  1. your Serializer is works
  2. the Deserializer is just works but it returned a LinkedHashMap instead of a object that you expected, right? and you can't convert that LinkedHashMap to your object.

I find the question transfers to How to Convert/Cast a LinkedHashMap to a Object, and you used ObjectMapper. If all situations can be confirmed, I found here a good post may be answer your question Casting LinkedHashMap to Complex Object

mapper.convertValue(desiredObject, new TypeReference<type-of-desiredObject>() { })

ObjectMapper's API at [here](https://fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/fasterxml/jackson/databind/ObjectMapper.html#convertValue(java.lang.Object, com.fasterxml.jackson.core.type.TypeReference))

And I hopes I don't missing your intention, and you can complement necessary situations, so someone or me can improve this answer.

Community
  • 1
  • 1
南山竹
  • 484
  • 8
  • 15
  • Thanks for your reply,but my serializer works like charm and converting my object to (JSON) bytes, and yes, deserializer is converting my object to LinkedHashMap which should be the desired object, also if I need to convert LinkedHashMap to desired object then what's the point of using custom deserilizer, I can just use StringDeserializer and covert the obtained JSON (as string) to desired object using ObjectMapper – Akhil Apr 21 '17 at 09:39
  • ```mapper.convertValue(desiredObject, new TypeReference() { })``` does work? – 南山竹 Apr 21 '17 at 09:44
  • yeah, that works, but this is the manual step which is required after consumer receive message, ideally the consumer should get the desired object which is User object in my case and not an object of LinkedHashMap. – Akhil Apr 21 '17 at 09:47
  • now, I know you wan to transfer the User object to your downstream, ok. and the previous ```code``` did your job? – 南山竹 Apr 21 '17 at 09:51
  • I see previous code as a workaround to solve this issue. – Akhil Apr 21 '17 at 09:53