0

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

dsk
  • 615
  • 1
  • 7
  • 19

3 Answers3

1

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.

Community
  • 1
  • 1
Shear Plane
  • 124
  • 8
1

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; 
    }
}
Shear Plane
  • 124
  • 8
Rahul
  • 15,979
  • 4
  • 42
  • 63
0

I used this solution for generic type class explanation And Spring implementation is here for Rest Template

dsk
  • 615
  • 1
  • 7
  • 19