问题描述:

I'm trying to get deserialization of subclasses working in Jackson. I defined a simple POJO and some sublcasses, but I'm getting the following exception when deserializing:

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

Here are the 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;

}

}

And this is the subclass:

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;

}

}

It's an Android application and the POJO's package is imported as external. Serialization/deserialization is managed by Spring Android's RestTemplate. I think it could be a class loader issue but I have no idea on why it occurs and how to solve it.

I also tried this simple test in Java SE and all works fine:

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

}

}

}

Any suggestion?

相关阅读:
Top