我想将jackson json库用于通用方法,如下所示:
public MyRequest tester() {
TypeReference> typeRef = new TypeReference>();
MyWrapper requestWrapper = (MyWrapper) JsonConverter.fromJson(jsonRequest, typeRef);
return requestWrapper.getRequest();
}
…
public class MyWrapper {
private MyRequest request;
public MyRequest getRequest() {
return request;
}
public void setRequest(MyRequest request) {
this.request = request;
}
}
public class MyRequest{
private List myobjects;
public void setMyObjects(List ets) {
this.myobjects = ets;
}
@NotNull
@JsonIgnore
public T getMyObject() {
return myobjects.get(0);
}
}
现在的问题是,当我调用位于请求对象内的getMyObject()时,杰克逊将嵌套的自定义对象作为LinkedHashMap返回。有什么方法可以指定需要返回T对象吗?例如:如果我发送了类型为Customer的对象,则应该从该List?中返回Customer。