问题描述
package com.utils;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import javax.xml.bind.annotation.*;
public class Test {
public static void main(String[] args) throws Exception {
XmlMapper xmlMapper = new XmlMapper();
String dogString = "<Dog _class=\"com.utils.Dog\" id=\"Dog123\">\n" +
"\t<name>Spot</name>\n" +
"</Dog>";
Dog dog = xmlMapper.readValue(dogString,Dog.class);
System.out.println(dog);
String ownerString ="<Owner _class=\"com.utils.Owner\" id=\"Owner123\">\n" +
"\t<animal>Dog123</animal>\n" +
"</Owner>";
Owner owner = xmlMapper.readValue(ownerString,Owner.class);
System.out.println(owner);
}
}
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,include = JsonTypeInfo.As.PROPERTY)
@XmlAccessorType(XmlAccesstype.FIELD)
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class),@JsonSubTypes.Type(value = Owner.class),})
abstract class MyObject {
@XmlAttribute
@XmlID
String id = getClass().getSimpleName() + "123";
@JsonProperty("id")
public String getId() {
return id;
}
}
abstract class Animal extends MyObject {
@JsonProperty("name")
String name;
public Animal(String name) {
this.name = name;
}
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,resolver = SimpleObjectIdResolver.class,property = "id",scope = Dog.class)
class Dog extends Animal {
@JsonCreator
public Dog(@JsonProperty("name") String name) {
super(name);
}
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,scope = Owner.class)
class Owner extends MyObject {
@XmlIDREF
Animal animal;
@JsonCreator
public Owner(@JsonProperty("animal") Animal animal) {
this.animal = animal;
}
public Animal getAnimal() {
return animal;
}
}
大家好,
我得到的堆栈跟踪如下:
Exception in thread "main" com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Could not resolve Object Id [Dog123] -- unresolved forward-reference?
at [Source: (StringReader); line: 2,column: 25] (through reference chain: com.utils.Owner["animal"])
at com.fasterxml.jackson.databind.deser.AbstractDeserializer._deserializefromObjectId(AbstractDeserializer.java:334)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:246)
...
这是构造函数不接受抽象类型的问题吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)