Quick revision
The short answer
Java erases generic type arguments at runtime, so List.class tells Jackson only that we want a list, not a list of what. With no element type, Jackson builds each element as a LinkedHashMap, and the cast to Order fails later when we call get(0). We fix it by passing the full type: mapper.readValue(json, new TypeReference<List<Order>>() {}), whose anonymous subclass keeps the type argument, or a JavaType from constructCollectionType, or simply Order[].class; with RestClient or RestTemplate the same fix is ParameterizedTypeReference<List<Order>>.
What to remember
In this answer, we saw that generics are erased, so List.class gives Jackson no element type and it builds LinkedHashMaps, with the ClassCastException appearing only on first use. We fixed it with new TypeReference<List<Order>>() {}, whose anonymous subclass keeps the type argument, with a JavaType from constructCollectionType when the type is known only at runtime, with Order[].class, and with ParameterizedTypeReference in RestClient and RestTemplate.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java erases generic type arguments at runtime, so List.class tells Jackson only that we want a list, not a list of what. With no element type, Jackson builds each element as a LinkedHashMap, and the cast to Order fails later when we call get(0). We fix it by passing the full type: mapper.readValue(json, new TypeReference<List<Order>>() {}), whose anonymous subclass keeps the type argument, or a JavaType from constructCollectionType, or simply Order[].class; with RestClient or RestTemplate the same fix is ParameterizedTypeReference<List<Order>>.
Why this question is asked
This is one of the first Jackson bugs almost every Java developer meets, and its symptom is confusing: the exception does not happen in readValue but a line later, in code that looks perfectly typed. Understanding it requires knowing what Java generics really are at runtime, which is why interviewers like it. In this answer, we will recap type erasure, reproduce the problem and see exactly where it fails, explain why Jackson builds maps, and then go through the fixes: TypeReference, JavaType, arrays and the Spring HTTP client equivalents.
1A Quick Recap of Type Erasure
Generics in Java are checked by the compiler and then erased. At runtime, a List<Order> and a List<String> are the same class, java.util.ArrayList or whatever implementation we use, with no record of their element type. List.class is just the raw class List; there is no such thing as List<Order>.class.
There is one important exception. When a class extends a generic type with a concrete argument, for example class OrderList extends ArrayList<Order>, the compiler records that superclass, including <Order>, in the class file, and reflection can read it back. That small exception is the trick behind TypeReference.
2Reproducing the Problem
Here is the failing code with what happens on each line.
7 messages in order. Play them, or step through them one at a time.
Jackson does exactly what it was asked; the wrong type surfaces only on first use.
Notice that readValue succeeds. The compiler accepted the assignment because readValue(String, Class<T>) returns a raw List here, and the cast to Order only happens where we use an element as an Order. That is why the stack trace points to a line that has nothing to do with Jackson, and why the bug can surface far away from where the JSON was read, even in another class.
3Why Jackson Builds LinkedHashMaps
Jackson receives List.class and nothing more. For the elements, the only type it knows is Object. When Jackson deserializes a JSON object into Object, it uses its natural mapping for untyped JSON: objects become LinkedHashMap, arrays become ArrayList, strings become String, numbers become Integer, Long or Double, and booleans become Boolean. LinkedHashMap is chosen because it keeps the order of the fields. So Jackson did not fail; it built the best structure it could with the information it had.
4Fix 1: TypeReference
TypeReference is an abstract generic class in Jackson. When we write new TypeReference<List<Order>>() {}, the braces create an anonymous subclass whose superclass is TypeReference<List<Order>>. As we saw, a superclass's type arguments survive in the class file, so Jackson reads List<Order> back through reflection and knows the element type.
Notice the {} at the end. Without it, the code would not compile because TypeReference is abstract, and that is the point: the anonymous subclass is what carries the type.
5Fix 2: JavaType and Arrays
Sometimes the element type is only known at runtime, for example in a generic helper that receives a Class<T>. A TypeReference cannot help there, because it captures what is written in the source. Instead we build a JavaType, Jackson's own full description of a type, from the TypeFactory.
Arrays avoid the problem entirely, because unlike generics, Java arrays know their element type at runtime: Order[].class is a real, distinct class. Reading into an array and wrapping it in a list is a short fix when no generic container is needed.
6The Same Problem in RestClient and RestTemplate
Spring's HTTP clients use Jackson underneath, so they have the same blind spot. restTemplate.getForObject(url, List.class) returns a list of maps for the same reason. Spring's answer is ParameterizedTypeReference, which uses the same anonymous subclass trick as TypeReference.
The unchecked warning on the original line was the compiler's hint. Treating unchecked warnings as worth reading, or failing the build on them in new code, catches this bug before it runs.
7Follow-up questions
Why does the error say LinkedHashMap and not HashMap?
Because Jackson's default for untyped JSON objects is LinkedHashMap, which keeps the fields in the order they appeared in the document. That makes the untyped result predictable when printed or serialized again.
Would a wrapper class solve it?
Yes. A class such as record OrderList(List<Order> orders) has a field whose declared type includes the element type, and Jackson reads field types through reflection, where generic field signatures are kept. Many APIs return an object with an items field for this and other reasons.
Does Kotlin or a newer Java version fix erasure?
Java still erases generics, including in Java 25. Kotlin can capture types at call sites with reified type parameters, which the Jackson Kotlin module uses for readValue<List<Order>>(), but underneath it builds the same kind of full type description.
8Conclusion
In this answer, we saw that generics are erased, so List.class gives Jackson no element type and it builds LinkedHashMaps, with the ClassCastException appearing only on first use. We fixed it with new TypeReference<List<Order>>() {}, whose anonymous subclass keeps the type argument, with a JavaType from constructCollectionType when the type is known only at runtime, with Order[].class, and with ParameterizedTypeReference in RestClient and RestTemplate.
Open with
This is type erasure. At runtime there is no List of Order, only List, so passing List.class tells Jackson to build a list but nothing about the elements.
Then, if they want more
For elements it only knows Object, and Jackson's natural mapping for a JSON object into Object is a LinkedHashMap. So readValue succeeds and returns a list of maps; the ClassCastException only appears when we call get 0 and the compiler's hidden cast to Order runs, which is why the stack trace points away from Jackson.
The usual fix is mapper.readValue with new TypeReference of List of Order, with the braces. Those braces create an anonymous subclass, and a superclass's type arguments are kept in the class file, so Jackson can read List of Order back by reflection.
When the element type is only known at runtime, I build a JavaType with getTypeFactory().constructCollectionType of List.class and Order.class. Another simple option is reading into Order[].class, because arrays keep their element type at runtime. The same problem exists in Spring's clients, where RestClient's body or RestTemplate's exchange take a ParameterizedTypeReference of List of Order.
And I would mention that the unchecked warning on the original line was the hint.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "This code throws ClassCastException: java.util.LinkedHashMap cannot be cast to Order:": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/494550c888/