I am rewriting the question since I figured out the actual error in the code.
This is a fully functional example of my issue (I am using Jackson 2.9.0):
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class MainClass {
public static class SubClass<TYPE> {
private List<TYPE> values;
public List<TYPE> getValues() {
return values;
}
public void setValues(List<TYPE> values) {
this.values = values;
}
}
public static class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private SubClass<Foo> propertyFoo;
public SubClass<Foo> getPropertyFoo() {
return propertyFoo;
}
public void setPropertyFoo(SubClass propertyFoo) {
this.propertyFoo = propertyFoo;
}
public static void main(String args[]) throws IOException {
URL url = System.class.getResource("/testFoo.json");
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerFor(MainClass.class);
MainClass mainClass = reader.readValue(url);
mainClass.getPropertyFoo().getValues().forEach(foo -> {
System.out.println(String.format("name: %s", foo.getName()));
});
}
}
Note the missing type parameter:
public void setPropertyFoo(SubClass propertyFoo)
instead of
public void setPropertyFoo(SubClass<Foo> propertyFoo)
The first form compiles but produces the following exception when run
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MainClass$Foo