杰克逊真的无法将json反序列化为泛型? [英] Is Jackson really unable to deserialize json into a generic type?

查看:263
本文介绍了杰克逊真的无法将json反序列化为泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个重复的问题,因为以下问题要么混乱,要么根本没有答案:

反序列化 - 通用类型与杰克逊



jackson-deserialize-into-runtime-specified-class



jackson-deserialize-using-generic-class



jackson-deserialize-generic-class-变量



我希望这个问题最终能够找到答案,使其变得清晰。



<有一个模型:

  public class AgentResponse< T> {

私人T的结果;

public AgentResponse(T结果){
this.result = result;
}
public T getResult(){
return result;


JSON输入:

  {result:{first-client-id:3,test-mail-module:3,third-client-id: 3,second-client-id:3}} 

以及两种推荐的反序列化类型:


$ b

  mapper.readValue(out,new TypeReference< AgentResponse< Map< String,Integer>>>(){} ); 

  JavaType javaType = mapper.getTypeFactory()。constructParametricType(AgentResponse.class,Map.class); 
mapper.readValue(out,javaType);



杰克逊永远不能处理泛型类型T,它认为它是JavaType中的Map,但它会因类型擦除而发现Object type构造函数参数并引发错误。那么这是一个杰克逊错误,还是我做错了什么?什么是TypeReference或JavaType的明确规范?

  com.fasterxml.jackson.databind.JsonMappingException:找不到合适的构造函数类型[简单类型,类com.fg.mail.smtp.AgentResponse< java.util.Map< java.lang.String,java.lang.Integer>>]:无法从JSON对象实例化(需要添加/启用类型信息?)
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)


解决方案

您需要在构造函数中添加一些注释告诉杰克逊如何构建这个对象。以下方法适用于我:

  public class AgentResponse< T> {

私人T的结果;

@JsonCreator
Public AgentResponse(@JsonProperty(result)T result){
this.result = result;
}
public T getResult(){
return result;如果没有 @JsonCreator
$ b

/ code>注释,杰克逊不知道调用这个构造函数。没有 @JsonProperty 注解,Jackson不知道构造函数的第一个参数映射到结果属性。

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反序列化为泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆