JAXB: Can't unmarshal JAXBElement<?>, getting ElementNSImpl instead

不羁岁月 提交于 2019-12-01 00:34:50

Running example:

(Hint: JAXBHelper are from utils-apl-derived, but are just short forms of you unmarshalling and marshalling routines)

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.junit.Test;
import org.omnaest.utils.xml.JAXBXMLHelper;

public class JAXBAnyElementTest
{
  @XmlType
  @XmlAccessorType(XmlAccessType.FIELD)
  protected static class TestSubEntity
  {
    /* ********************************************** Variables ********************************************** */
    @XmlElement
    private String fieldString  = null;
    @XmlElement
    private int    fieldInteger = -1;

    /* ********************************************** Methods ********************************************** */

    /**
     * @see TestSubEntity
     */
    public TestSubEntity()
    {
      super();
    }

    /**
     * @see TestSubEntity
     * @param fieldString
     * @param fieldInteger
     */
    public TestSubEntity( String fieldString, int fieldInteger )
    {
      super();
      this.fieldString = fieldString;
      this.fieldInteger = fieldInteger;
    }

    /**
     * @return the fieldString
     */
    public String getFieldString()
    {
      return this.fieldString;
    }

    /**
     * @param fieldString
     *          the fieldString to set
     */
    public void setFieldString( String fieldString )
    {
      this.fieldString = fieldString;
    }

    /**
     * @return the fieldInteger
     */
    public int getFieldInteger()
    {
      return this.fieldInteger;
    }

    /**
     * @param fieldInteger
     *          the fieldInteger to set
     */
    public void setFieldInteger( int fieldInteger )
    {
      this.fieldInteger = fieldInteger;
    }

    @Override
    public int hashCode()
    {
      final int prime = 31;
      int result = 1;
      result = prime * result + this.fieldInteger;
      result = prime * result + ( ( this.fieldString == null ) ? 0 : this.fieldString.hashCode() );
      return result;
    }

    @Override
    public boolean equals( Object obj )
    {
      if ( this == obj )
      {
        return true;
      }
      if ( obj == null )
      {
        return false;
      }
      if ( !( obj instanceof TestSubEntity ) )
      {
        return false;
      }
      TestSubEntity other = (TestSubEntity) obj;
      if ( this.fieldInteger != other.fieldInteger )
      {
        return false;
      }
      if ( this.fieldString == null )
      {
        if ( other.fieldString != null )
        {
          return false;
        }
      }
      else if ( !this.fieldString.equals( other.fieldString ) )
      {
        return false;
      }
      return true;
    }

  }

  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  protected static class TestEntity
  {
    @XmlElements({ @XmlElement(name = "int", type = Integer.class), @XmlElement(name = "string", type = String.class),
        @XmlElement(name = "testsubentity", type = TestSubEntity.class), @XmlElement(name = "other") })
    private List<Object> anyElementList = null;

    /**
     * @return the anyElementList
     */
    public List<Object> getAnyElementList()
    {
      return this.anyElementList;
    }

    /**
     * @param anyElementList
     *          the anyElementList to set
     */
    public void setAnyElementList( List<Object> anyElementList )
    {
      this.anyElementList = anyElementList;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
      final int prime = 31;
      int result = 1;
      result = prime * result + ( ( this.anyElementList == null ) ? 0 : this.anyElementList.hashCode() );
      return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals( Object obj )
    {
      if ( this == obj )
      {
        return true;
      }
      if ( obj == null )
      {
        return false;
      }
      if ( !( obj instanceof TestEntity ) )
      {
        return false;
      }
      TestEntity other = (TestEntity) obj;
      if ( this.anyElementList == null )
      {
        if ( other.anyElementList != null )
        {
          return false;
        }
      }
      else if ( !this.anyElementList.equals( other.anyElementList ) )
      {
        return false;
      }
      return true;
    }

  }

  @SuppressWarnings("cast")
  @Test
  public void test()
  {
    //
    final TestEntity testEntity = new TestEntity();

    //
    final List<Object> anyElementList = Arrays.asList( (Object) "a", Integer.valueOf( 1 ), Boolean.valueOf( true ),
                                                       new TestSubEntity( "field1", 1 ) );
    testEntity.setAnyElementList( anyElementList );

    //
    final String objectAsXML = JAXBXMLHelper.storeObjectAsXML( testEntity );
    System.out.println( objectAsXML );

    //
    final TestEntity objectFromXML = JAXBXMLHelper.loadObjectFromXML( objectAsXML, TestEntity.class );
    assertEquals( testEntity, objectFromXML );
  }
}

Output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<testEntity>
    <string>a</string>
    <int>1</int>
    <other xsi:type="xs:boolean" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">true</other>
    <testsubentity>
        <fieldString>field1</fieldString>
        <fieldInteger>1</fieldInteger>
    </testsubentity>
</testEntity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!