1

I have written the following method that takes a generic type. How do i pass a list of type Order as the response type?

public <T> ServiceResponse<T> fetchGet(String url, Class<T> responseType, boolean authRequired);

// This works just fine
fetchGet("MY_URL", Order.class, true);

// How do i pass a list?
fetchGet("MY_URL", List<Order>.class, true);

2 Answers 2

3

I ended up using an array instead of a list. I had to pass this to RestTemplate exchange method, and passing an array instead of List worked.

fetchGet("MY_URL", Order[].class, true);
Sign up to request clarification or add additional context in comments.

Comments

2

You're going to have to use List.class and do an explicit cast afterwards. Or you could do the alternate hack of calling fetchGet("MY_URL", (Class<List<Order>>) (Class) List.class, true);.

2 Comments

I'm wondering if jackson framework would know what generic type it should convert my data to. Will give it a try
Cannot cast Class<List> to Class<List<Order>>>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.