I want to use the Jackson JSON library for a generic method as follows:
public MyRequest<T> tester() { TypeReference<MyWrapper<T>> typeRef = new TypeReference<MyWrapper<T>>(); MyWrapper<T> requestWrapper = (MyWrapper<T>) JsonConverter.fromJson(jsonRequest, typeRef); return requestWrapper.getRequest(); }
...
public class MyWrapper<T> { private MyRequest<T> request; public MyRequest<T> getRequest() { return request; } public void setRequest(MyRequest<T> request) { this.request = request; } } public class MyRequest{ private List<T> myobjects; public void setMyObjects(List<T> ets) { this.myobjects = ets; } @NotNull @JsonIgnore public T getMyObject() { return myobjects.get(0); } }
Now the problem is that when I call getMyObject (), which is inside the request object, Jackson returns the nested user object as a LinkedHashMap. Is there a way to indicate that the object T should be returned? For example: if I sent an object of type Customer, then Customer should be returned from this list ?.
Thank you
java json generics jackson
techzen Jul 27 2018-11-14T00: 00Z
source share