我正在尝试从引用自己的Comment对象的网络中获取JSON,并通过读取JSON获得相同的Web。
这是Comment类:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Comment {
private int id;
private String text;
private Collection<Comment> parents;
private Collection<Comment> replies;
private static int next_id = 0;
public Comment(final int id, final String text, final Collection<Comment> parents, final Collection<Comment> replies) {
super();
this.id = id;
this.text = text;
this.parents = parents;
this.replies = replies;
}
public Comment(final String text) {
Comment.next_id++;
this.id = Comment.next_id;
this.text = text;
this.parents = new HashSet<Comment>();
this.replies = new HashSet<Comment>();
}
public int getId() {
return this.id;
}
public void setId(final int id) {
this.id = id;
}
public String getText() {
return this.text;
}
public void setText(final String text) {
this.text = text;
}
public Collection<Comment> getParents() {
return this.parents;
}
public void setParents(final HashSet<Comment> parents) {
this.parents = parents;
}
public void addParent(final Comment c) {
this.parents.add(c);
}
public Collection<Comment> getReplies() {
return this.replies;
}
public void setReplies(final HashSet<Comment> replies) {
this.replies = replies;
}
public void addReply(final Comment c) {
this.replies.add(c);
}
public static int getNext_id() {
return Comment.next_id;
}
public static void setNext_id(final int next_id) {
Comment.next_id = next_id;
}
@Override
public String toString() {
return "Comment [id=" + this.id + ", text=" + this.text + ", replies=" + this.replies + "]";
}
我在这里创建一些评论并将其链接:
public class CommentsJSONGenerator {
public static void main(final String[] args) throws JsonGenerationException, JsonMappingException, IOException {
final Comment c1 = new Comment("This is a sample comment");
final Comment c2 = new Comment("This is an answer for the first comment");
final Comment c3 = new Comment("This is second answer for the first comment");
final Comment c4 = new Comment("This is an answer for the third comment");
c1.addReply(c2);
c1.addReply(c3);
c2.addParent(c1);
c3.addParent(c1);
c3.addReply(c4);
c4.addParent(c3);
final ObjectMapper m = new ObjectMapper();
final ObjectWriter w = m.writer(new DefaultPrettyPrinter());
w.writeValue(new File("src/main/resources/comments.json"), c1);
}
这是我得到的JSON:
{
"id" : 1,
"text" : "This is a sample comment",
"parents" : [ ],
"replies" : [ {
"id" : 2,
"text" : "This is an answer for the first comment",
"parents" : [ 1 ],
"replies" : [ ]
}, {
"id" : 3,
"text" : "This is second answer for the first comment",
"parents" : [ 1 ],
"replies" : [ {
"id" : 4,
"text" : "This is an answer for the third comment",
"parents" : [ 3 ],
"replies" : [ ]
} ]
} ]
}
之后,我尝试做相反的事情,从JSON获取相同的对象。
public class JSONReader {
public static void main(final String[] args) throws JsonParseException, JsonMappingException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final Comment c = mapper.readValue(new File("src/main/resources/comments.json"), Comment.class);
}
}
但是它抛出了这个异常:
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `domain.Comment` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)