简体   繁体   English

从泛型类型获取类

[英]get class from generic type

how can I call .class Or how can I get class from following generic type 我该如何调用.class或如何从以下通用类型获取类

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

It shows error 显示错误

As far as I know the information is not avaiable at runtime. 据我所知,该信息在运行时不可用。 But perhaps, you can use some ideas from Get generic type of class at runtime . 但是也许,您可以在运行时使用“ 获取类的通用类型”中的一些想法。

No, you cannot. 你不能。

The generic type T is not kept at runtime because of Type Erasure . 由于类型Erasure ,泛型T不会在运行时保留。

However, if you still want to do it 但是,如果您仍然想这样做

public class Foo<T> 
{
    private Class<T> type;

    public Foo(Class<T> type) { 
        this.type = type; 
    }
}

我使用此解决方案进行通用类型类解释,而Spring实现在此处用于Rest Template

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM