java泛型问题,急救

发布于 2021-11-23 09:50:10 字数 817 浏览 873 评论 2

import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonReader<T> {

public static <T> T readValue(String json) {
	ObjectMapper mapper = new ObjectMapper();
	try {
		TypeReference<T> ref = new TypeReference<T>() {};
		System.out.println(ref.getClass());
		System.out.println(ref.getType());
		return mapper.readValue(json, ref);
	} catch (JsonParseException e) {
		e.printStackTrace();
	} catch (JsonMappingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
}
请问这里哪里错了,TypeReference<T>得不到T的具体类型

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2021-11-24 15:11:30
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
Type[] types = type.getActualTypeArguments();
Class<T> subClass = (Class<T>) types[0];

冷弦 2021-11-23 23:56:34
Class<T> subClass = (Class<T>) ((ParameterizedType) (this.getClass().getGenericSuperclass())).getActualTypeArguments()[0];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文