0

我正在开发一个带有 RESTful Web 服务的 GWT Web 应用程序。使用 Jackson 1.8 将 Web 服务的结果反序列化为 POJO。它适用于简单的字段。但是,当它尝试反序列化 POJO 列表时会失败。这是带有要反序列化的列表的 POJO:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DatosIndicadoresSVclaveDTO implements Serializable {
    ...
    @XmlAttribute
    @JsonDeserialize(contentAs = IdeologicoVOXDTO.class)
    public List<IdeologicoVOXDTO> ideologicoVox;
    ...

    //getter/setters
}

这是包含列表的 POJO

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class IdeologicoVOXDTO implements Serializable {
    @XmlAttribute
    private Integer numeroPalabra;
    @XmlAttribute
    private String palabra;
    @XmlAttribute
    private Integer categoria;
    ...

    //getter/setters
}

JSON具有以下结构:

{datosIndicadoresSVclave: {
        ...
        "ideologicoVox":[
            {
                "categoria":"1", 
                "numeroPalabra":"1", 
                "palabra":"abandonado", 
                ...
            },
            {
                "categoria":"2", 
                "numeroPalabra":"3", 
                "palabra":"hambre", 
                ...
            }
        ],
        ...
    }
}

运行时,Web 服务的结果运行良好,但反序列化会打印此错误:

SEVERE: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@10b61ad; line: 1, column: 580] (through reference chain: org.ull.etsii.client.dto.DatosIndicadoresSVclaveDTO["ideologicoVox"]) at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)

Any idea?? Thanks!!

4

2 回答 2

0

I had faced some similar kind of problem and tried the following way, and it worked for me.

Create a new class which contains property ~ public List ideologicoVox ~

And use this class reference as property to the main class i.e ~ DatosIndicadoresSVclaveDTO

于 2013-04-27T10:56:59.127 回答
0

I've solved!!

The problem is the size list is variable, and it fails if it has one element. The Jackson's version is 1.7, that it can't accept arrays single value. My solution is GSON with a custom register type, and I've used the Joshi's adviced. It works great!! Thanks!!

于 2013-05-03T06:49:33.837 回答