杰克逊和泛型类型引用
我想使用jackson json库作为泛型方法,如下所示:
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<T> {
private List<T> myobjects;
public void setMyObjects(List<T> ets) {
this.myobjects = ets;
}
@NotNull
@JsonIgnore
public T getMyObject() {
return myobjects.get(0);
}
}
现在的问题是,当我调用请求对象内的getMyObject()时,Jackson会将嵌套的自定义对象作为LinkedHashMap返回。有没有办法指定需要返回 T 对象?例如:如果我发送了客户类型的对象,那么客户应该从该列表中返回吗?