我同時使用enableDefaultTyping並提供一個通用的TypeRefernce時遇到了反序列化問題。傑克遜似乎無法確定哪種類型的信息更重要。這測試用例demonstarting問題:傑克遜泛型類型引用+ enableDefaultTyping問題
@Test
public void roundTripTest() throws JsonGenerationException,
JsonMappingException, IOException {
// 0 Value Test
Integer[] integers = new Integer[] {};
Wrap<Integer[]> beforeResult = new Wrap<Integer[]>(integers);
File file = new File("/tmp/jsonTest");
mapper.writeValue(file, beforeResult);
TypeReference<Wrap<Integer[]>> typeRef = new TypeReference<JacksonMapperTest.Wrap<Integer[]>>() {
};
Wrap<Integer[]> afterResult = mapper.readValue(file, typeRef);
assertNotNull(afterResult);
}
public static class Wrap<T> {
private T wrapped;
public Wrap() {
}
public Wrap(T wrapped) {
this.wrapped = wrapped;
}
public T getWrapped() {
return wrapped;
}
public void setWrapped(T wrapped) {
this.wrapped = wrapped;
}
}
其中映射是:
mapper = new ObjectMapper();
mapper.enableDefaultTyping();
和例外是:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class JacksonMapperTest$Wrap<[Ljava.lang.Integer;>]: can not instantiate from JSON object (need to add/enable type information?)
很奇怪,不是嗎?通過使用beforeResult.getClass而不是TypeRefernce,可以忽略該問題,但它仍然不是首選的行爲。
我錯過任何種類的選擇爲了解決這個問題?
我用傑克遜1.9.3
[編輯] 使用地圖不是數組作爲包裝的對象按預期工作!