问题出在你的TypeReference<Map<String, T>>
. 当你在这里给 T 时,杰克逊会尝试推断类型。由于此时杰克逊对学生类一无所知,因此它推断“名称”:“名称1”是一个具有键“名称”和值“名称1”的LinkedHashMap。所以反过来它会创建一个LinkedHashMap<String, LinkedHashMap<String>>
.
作为对象映射器方法中的快速解决方案,您可以使用 TypeReference 作为
studentMap = mapper.readValue(new File(filename), new TypeReference<Map<String, Student>>() { });
由于该方法是 getStudents ,因此使用此 TypeReference 是有意义的,但是该方法中泛型的全部意义都被浪费了。
另一种方法是使用自定义反序列化器。有很多方法可以做到这一点。您可以在http://wiki.fasterxml.com/JacksonPolymorphicDeserialization中找到详细信息
例如,让所有可能的类的标记接口使用自定义反序列化器。为你说场景假设我们有一个接口 StudentI 将由你键入 T 可能具有的所有可能的类实现。然后使用@JsonTypeInfo 提供有关该类的其他详细信息
学生界面
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Student.class, name = "student") })
public interface StudentI {
}
这意味着如果您在 json 中提供类似 'type : student' 的内容,映射器将为您使用 Student.class。
样本类
import java.io.File;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SampleClass {
public static <T extends StudentI> Map<String, T> getStudents(String filename) {
Map<String, T> studentMap = null;
ObjectMapper mapper = new ObjectMapper();
try {
studentMap = mapper.readValue(new File(filename),
new TypeReference<Map<String, StudentI>>() { });
} catch (JsonParseException e) {
System.out.println(e.getMessage());
} catch (JsonMappingException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return (Map<String, T>)studentMap;
}
}
学生.json
{
"student1" :
{
"name" : "name1",
"age" : 22,
"type" : "student"
},
"student2" :
{
"name" : "name2",
"age" : 22,
"type" : "student"
}
}
现在你的 Student 类应该实现这个接口
public class Student implements StudentI {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
完成此操作后,您的代码将按预期工作
public static void main(String[] args) {
Map<String, Student> students = SampleClass.getStudents("students.json");
System.out.println(students.get("student1").getName());
}
//Output : name1