更新时间:2023-01-16 21:47:25
你需要在构造函数上添加一些注解来告诉 Jackson 如何构建对象.以下对我有用:
公共类 AgentResponse{私人T结果;@JsonCreatorpublic AgentResponse(@JsonProperty("result") T result) {this.result = 结果;}公共 T getResult() {返回结果;}}
如果没有 @JsonCreator
注释,Jackson 无法知道调用这个构造函数.如果没有 @JsonProperty
注释,Jackson 不知道构造函数的第一个参数映射到 result
属性.
This is a duplicate question because the following questions are either messy or they are not answered at all:
deserializing-a-generic-type-with-jackson
jackson-deserialize-into-runtime-specified-class
jackson-deserialize-using-generic-class
jackson-deserialize-generic-class-variable
I hope that this question will finally find an answer that makes this clear for good.
Having a model :
public class AgentResponse<T> {
private T result;
public AgentResponse(T result) {
this.result = result;
}
public T getResult() {
return result;
}
}
JSON input:
{"result":{"first-client-id":3,"test-mail-module":3,"third-client-id":3,"second-client-id":3}}
and two recommended ways of deserializing generic types :
mapper.readValue(out, new TypeReference<AgentResponse<Map<String, Integer>>>() {});
or
JavaType javaType = mapper.getTypeFactory().constructParametricType(AgentResponse.class, Map.class);
mapper.readValue(out, javaType);
Jackson is never able to deal with the generic type T, it figures it's a Map from JavaType, but it finds Object type constructor argument because of type erasure and throws an error. So is this a Jackson bug, or am I doing something wrong? What else is explicit specification of TypeReference or JavaType for?
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.fg.mail.smtp.AgentResponse<java.util.Map<java.lang.String,java.lang.Integer>>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.InputStreamReader@4f2d26d; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2064)
You need to add some annotations on the constructor to tell Jackson how to build the object. The following worked for me:
public class AgentResponse<T> {
private T result;
@JsonCreator
public AgentResponse(@JsonProperty("result") T result) {
this.result = result;
}
public T getResult() {
return result;
}
}
Without the @JsonCreator
annotation, Jackson cannot know to call this constructor. And without the @JsonProperty
annotation, Jackson does not know that the first argument of the constructor maps to the result
property.
无法将当前 JSON 对象(例如 {“name":“value"})反序列化为类型“System.Collections.Generic.List"1
无法将当前 JSON 对象(例如 {“name":“value"})反序列化为“System.Collections.Generic.List"类型
使用 Jackson & 解析 JSON 文件中的内容时出现问题消息- JsonMappingException - 无法反序列化为超出 START_ARRAY 令牌
无法将 JSON 数组(例如 [1,2,3])反序列化为类型"",因为类型需要 JSON 对象(例如 {“name":“value"})才能正确反序列化
org.codehaus.jackson.map.JsonMappingException:无法将java.util.ArrayList的实例反序列化为START_OBJECT标记
'无法将当前JSON对象(例如{" Name":" value"})反序列化为类型'system.collections.generic.list`1 ...