1

我正在尝试反序列化在杰克逊工作的子类。我定义了一个简单的 POJO 和一些子类,但是在反序列化时出现以下异常:

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();
    }

}
}

有什么建议吗?

4

0 回答 0