解组嵌套结构时@XmlVariableNode失败

时间:2014-01-24 17:43:34

标签: java xml jaxb eclipselink moxy

Hi Blaise我发现@XmlVariableNode在解组嵌套结构失败时遇到了问题。编组工作正常,但解组失败。以下是一个例子。我使用的是moxy 2.5.2-M1版本。

@XmlRootElement(name = "person")
public class Person {

  String personId;

  @XmlElementWrapper(name = "friends")
  @XmlVariableNode("name")
  List<Friend> friends = new ArrayList<Friend>();

  @XmlElementWrapper(name = "enemies")
  @XmlVariableNode("name")
  List<Enemy> enemies = new ArrayList<Enemy>();

  public Person() {
    super();
  }

  public static void main(String[] args) {

    Person person = new Person();
    person.personId = "123";

    final List<Relative> realtives = new ArrayList<Relative>() {
      {
        add(new Relative("rabc", "rdef"));
      }
    };

    List<Friend> friends = new ArrayList<Friend>() {
      {
        add(new Friend("fabc", "fdef", realtives));
      }
    };

    person.friends = friends;

    List<Enemy> enemies = new ArrayList<Enemy>() {
      {
        add(new Enemy("eabc", "edef"));
      }
    };

    person.enemies = enemies;

    JaxbMarshallerUnMarshaller m = new JaxbMarshallerUnMarshaller();
    StringWriter writer = new StringWriter();

    m.marshal(person, writer, MediaType.APPLICATION_JSON_VALUE);

    System.out.print(writer.toString());

    Person person1 = m.unmarshal(writer.toString(), Person.class, MediaType.APPLICATION_JSON_VALUE);

  }
}





@XmlAccessorType(XmlAccessType.FIELD)
public class Friend {
  String name;
  String lastName;

  List<Relative> relatives;

  public Friend() {
    super();
  }

  public Friend(String name, String lastName, List<Relative> relatives) {
    super();
    this.name = name;
    this.lastName = lastName;
    this.relatives = relatives;
  }

}



@XmlAccessorType(XmlAccessType.FIELD)
public class Relative {

  String name;
  String lastName;

  public Relative() {
    super();
  }

  public Relative(String name, String lastName) {
    super();
    this.name = name;
    this.lastName = lastName;
  }

}

{
   "person" : {
      "friends" : {
         "fabc" : {
            "name" : "fabc",
            "lastName" : "fdef",
            "relatives" : [ {
               "name" : "rabc",
               "lastName" : "rdef"
            } ]
         }
      },
      "enemies" : {
         "eabc" : {
            "name" : "eabc",
            "lastName" : "edef"
         }
      }
   }
}

Exception in thread "main" Local Exception Stack: 
    Exception [EclipseLink-32] (Eclipse Persistence Services - 2.5.2.v20131113-a7346c6): org.eclipse.persistence.exceptions.DescriptorException
    Exception Description: Trying to set value [[com.test.dto.impl.Relative@39ed1b0b]] for instance variable [relatives] of type [java.util.List] in the object.  The specified object is not an instance of the class or interface declaring the underlying field, or an unwrapping conversion has failed.
    Internal Exception: java.lang.IllegalArgumentException: Can not set java.util.List field com.test.dto.impl.Friend.relatives to com.test.dto.impl.Enemy
    Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[relatives]
    Descriptor: XMLDescriptor(com.test.dto.impl.Friend --> [])
        at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruInstanceVariableAccessor(DescriptorException.java:703)
        at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:188)
        at org.eclipse.persistence.mappings.DatabaseMapping.setAttributeValueInObject(DatabaseMapping.java:1613)
        at org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping.setAttributeValueInObject(XMLCompositeCollectionMapping.java:740)
        at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.setContainerInstance(XMLCompositeCollectionMappingNodeValue.java:268)
        at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endDocument(UnmarshalRecordImpl.java:618)
        at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:184)
        at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:125)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:972)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:425)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:375)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:705)
        at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:655)
        at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:301)
        at com.test.dto.common.JaxbMarshallerUnMarshaller.unmarshal(JaxbMarshallerUnMarshaller.java:109)
        at com.test.dto.impl.Person.main(Person.java:66)
    Caused by: java.lang.IllegalArgumentException: Can not set java.util.List field com.test.dto.impl.Friend.relatives to com.test.dto.impl.Enemy
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
        at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
        at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:57)
        at java.lang.reflect.Field.set(Field.java:657)
        at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:141)
        ... 14 more

我尝试查看源代码,但无法确定究竟是什么问题?你能看一下吗?

0 个答案:

没有答案