I have a method, which uses generics as follows
public <T> ReturnThis<T> doSomething(Request req) {
.
.
T result = ObjectMapperFactory.getMapper().readValue(req, new TypeReference<T>() {});
.
.
return ReturnThis<T>;
}
I do understand that there are @JsonSubTypes that can be used to explicitly let the Jackson know about type. But that is not my use case over here.
Alternatively, this could also be done
public <T> ReturnThis<T> doSomething(Request req, TypeReference<T> type) {
.
.
T result = ObjectMapperFactory.getMapper().readValue(req, type);
.
.
return ReturnThis<T>;
}
Is the below one more preferred or more safer?