Jackson 和泛型类型参考
我想使用 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 对象吗?例如:如果我发送了 Customer 类型的对象,那么应该从该列表中返回 Customer 吗?
I want to use 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<T> {
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 custom object as a LinkedHashMap. Is there any way in which I specify that T object needs to be returned? For example: if I sent object of type Customer then Customer should be returned from that List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Java 类型擦除的一个众所周知的问题:T 只是一个类型变量,您必须指示实际的类,通常作为 Class 参数。如果没有这些信息,最好的办法就是使用边界;普通 T 与“T extends Object”大致相同。然后 Jackson 会将 JSON 对象绑定为地图。
在这种情况下,测试器方法需要访问类,您可以构造
然后
This is a well-known problem with Java type erasure: T is just a type variable, and you must indicate actual class, usually as Class argument. Without such information, best that can be done is to use bounds; and plain T is roughly same as 'T extends Object'. And Jackson will then bind JSON Objects as Maps.
In this case, tester method needs to have access to Class, and you can construct
and then
“JavaType”有效!
我试图将 json 字符串中的列表解组(反序列化)为 ArrayList java 对象,并且几天以来一直在努力寻找解决方案。
下面是最终给我解决方案的代码。
代码:
'JavaType' works !!
I was trying to unmarshall (deserialize) a List in json String to ArrayList java Objects and was struggling to find a solution since days.
Below is the code that finally gave me solution.
Code:
我修改了rushidesai1的答案以包含一个工作示例。
JsonMarshaller.java
输出
JsonMarshallerUnmarshaller.java
Station.java
data.json
I modified rushidesai1's answer to include a working example.
JsonMarshaller.java
Output
JsonMarshallerUnmarshaller.java
Station.java
data.json