从泛型类型获取类

时间:2016-03-15 10:04:16

标签: java generics

如何调用.class或者如何从通用类型

中获取类
 headerMap.put("accept", MediaType.APPLICATION_JSON_VALUE);
     ResponseEntity<PaginationResponse<Category>> response =
     restClient.getResource(url, headerMap, **PaginationResponse<Category>.class**);
     LOGGER.info("Method getAllCategories() - END");

显示错误

3 个答案:

答案 0 :(得分:1)

据我所知,这些信息在运行时是不可用的。 但也许,您可以使用Get generic type of class at runtime中的一些想法。

答案 1 :(得分:1)

不,你不能。

由于Type Erasure,泛型类型T不会在运行时保留。

但是,如果你还想这样做

app/init

答案 2 :(得分:0)

我将此解决方案用于泛型类explanation Rest模板

的Spring实现为here