#MongoDb + Jackson Deserializer issue

1 messages · Page 1 of 1 (latest)

versed terrace
#

I am using a generic type parameter T for fields like replyTo, cc, and bcc. SmsId class and EmailID class are the generic class that I am using in the NotificationTemplateRepoDto<?> class.
While retrieving from the db, the replyTo/cc/bcc field is deserialized into a LinkedList of LinkedHashMap, not a List<EmailId > or List<SmsId>.

Is there any efficient solution for this to convert the object to the specific generics.

@Document(collection = "NotificationTemplate")
@Data
public class NotificationTemplateRepoDto<T> implements Serializable {

private String id;
private String name;
private String category;
private String lob;
private Type type;
private boolean active;
private LocalDateTime created;
private LocalDateTime updated;
private String owner;
private String body;
private List<Container> attachments;
private List<T> bcc;
private List<T> cc;
private T replyTo;
private String mediaType;
}
public class SmsId implements Serializable {

private int countryCode;
private long mobileNo;
}

@NoArgsConstructor
public class EmailId implements Serializable {

private String name;
private String id;
}

public interface NotificationTemplateRepository<T>
extends ReactiveMongoRepository<NotificationTemplateRepoDto<T>, String> {
}

misty starBOT
#

<@&1004656351647117403> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I am using a generic type parameter T for fields like replyTo, cc, and bcc. SmsId class and EmailID class are the generic class that I am using in the NotificationTemplateRepoDto<?> class. While retrieving from the db, the replyTo/cc/bcc field is deseriali

It seems like you are facing a deserialization issue with MongoDB and Jackson when using generic type parameters in your fields like replyTo, cc, and bcc. One common solution to this problem is to use TypeReference class from Jackson to handle generic types during deserialization.

You can create a TypeReference object for your generic class like this:

TypeReference<SmsId> smsIdType = new TypeReference<SmsId>() {};

Then, you can use this TypeReference object during deserialization like this:

SmsId smsId = objectMapper.readValue(jsonString, smsIdType);

This should help Jackson properly deserialize your generic class instances. Additionally, make sure that your generic classes have default constructors or annotated constructors for Jackson to deserialize them correctly.

Useful links:

worldly summit
#

that's not going to work, as jackson doesn't know what T you're talking about

versed terrace
#

can you suggest me any alternate solution

worldly summit
#

you can make a basetype for smsid and emailid

#

an interface is enough, and then just use the interface instead of the generic T

#

I'm talking about 2.2

#

from the article

versed terrace
#

thankyou @worldly summit . I will look into it.