杰克逊在Android中进行反序列化:无效的类型ID,没有找到类

时间:2013-06-25 08:38:05

标签: android jackson resttemplate

我正在尝试对在Jackson工作的子类进行反序列化。我定义了一个简单的POJO和一些sublcasses,但是在反序列化时我得到以下异常:

06-25 10:06:32.765: E//RequestProcessor.java:251(26690): Caused by: java.lang.IllegalArgumentException: Invalid type id 'org.moser.pointer.rest.Query' (for id type 'Id.class'): no such class found

以下是Pojos:

package org.moser.pointer.rest;

import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
@JsonSubTypes({
@JsonSubTypes.Type(value=Query.class, name="org.moser.pointer.rest.Query"),
@JsonSubTypes.Type(value=Tip.class, name="org.moser.pointer.rest.Tip"),
})
public class Comment {

private String text;
private String authorThumbnailUrl;
private String username;
private String userid;

public Comment(){}

public Comment(String text, String author, String authorThumbnailUrl) {
    this.text=text;
    this.username=author;
    this.authorThumbnailUrl=authorThumbnailUrl;
}

public String getText() {
    return text;
}
public void setText(String text) {
    this.text = text;
}
public String getAuthorThumbnailUrl() {
    return authorThumbnailUrl;
}
public void setAuthorThumbnailUrl(String authorThumbnailUrl) {
    this.authorThumbnailUrl = authorThumbnailUrl;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}

public String getUserid() {
    return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}
}

这是子类:

package org.moser.pointer.rest;

public class Query extends Comment {

private long timeToLive;

public long getTimeToLive() {
    return timeToLive;
}
public void setTimeToLive(long timeToLive) {
    this.timeToLive = timeToLive;
}
}

这是一个Android应用程序,POJO的包作为外部导入。序列化/反序列化由Spring Android的RestTemplate管理。我认为这可能是一个类加载器问题,但我不知道它为什么会发生以及如何解决它。

我也在Java SE中尝试过这个简单的测试,一切正常:

public class JacksonTest {


public static void main(String args[]){

    ObjectMapper mapper = new ObjectMapper(); 

    Query query=new Query();
    query.setText("text");
    query.setAuthorThumbnailUrl("authorThumbnailUrl");
    query.setTimeToLive(0);
    query.setUserid("userId");
    query.setUsername("username");

    String json;
    try {
        json = mapper.writeValueAsString(query);
        System.out.println(json);
        Query read = (Query) mapper.readValue(json, Comment.class);
        System.out.println(mapper.writeValueAsString(read));  
    } catch (JsonGenerationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

有什么建议吗?

0 个答案:

没有答案