简体   繁体   中英

get class from generic type

how can I call .class Or how can I get class from following generic type

 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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