子类属性未映射到JAXB

时间:2015-07-13 19:28:53

标签: java xml jaxb

我似乎无法弄清楚为什么子类属性没有映射到父类属性。 Parent类的值为null。是否需要显式转换属性?我怀疑不应该。我相信我没有使用正确的XML注释。任何帮助,将不胜感激。

-C

主:

Converter converter = new Converter(ChildClass.COSTAR);

转换器类

public Converter(ParentClass iClass)
{
    mClass = iClass;
}

调试器的值:

mName = {java.lang.String@724}"Costar" // Child class
mRows = {java.lang.String@725}"16"     // Child class
mCols = {java.lang.String@726}"24"     // Child class
value = null                           // Parent class
columns = null                         // Parent class
rows = null                            // Parent class

家长班:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ParentClass", propOrder = {
"value"
})
public class ParentClass {

@XmlValue
protected String value;
@XmlAttribute
protected String columns;
@XmlAttribute
protected String rows;

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getRows() {
    return rows;
}

public void setRows(String value) {
    this.rows = value;
}

public String getColumns() {
    return columns;
}

public void setColumns(String value) {
    this.columns = value;
}

}

儿童班:

public class ChildClass
       extends ParentClass
{

public static final ChildClass COSTAR = new ChildClass("Costar", 16, 24);

public static final ChildClass LOSTAR = new ChildClass("Lostar",8,12);

public static final ChildClass JOSTAR = new ChildClass("Jostar",16,24);

@XmlValue
private String mName;
@XmlAttribute
private String mRows;
@XmlAttribute
private String mCols;

public ChildClass(String iName, int iRows, int iCols)
{
    if(iName == null)
    {
       throw new IllegalArgumentException("Cannot pass in null name");
    }

    if(iRows<1)
    {
       throw new IllegalArgumentException("Invalid plate row dimension: " + iRows);
    }

    if(iCols<1)
    {
       throw new IllegalArgumentException("Invalid plate column dimension: " + iCols);
    }

    mName = iName;
    mRows = String.valueOf(iRows);
    mCols = String.valueOf(iCols);

}

@Override
public String getValue()
{
    return mName;
}

@Override
public void setValue(String value)
{
    throw new IllegalArgumentException("Cannot reset plate mName");
}

@Override
public String getRows()
{
    return mRows;
}

@Override
public void setRows(String value)
{
    throw new IllegalArgumentException("Cannot reset plate mRows");
}

@Override
public String getColumns()
{
    return mCols;
}

@Override
public void setColumns(String value)
{
    throw new IllegalArgumentException("Cannot reset plate cols");
}

}

修改

使用调试器时,它不会显示将Child类转换为Parent类。它保留为Child类对象。

1 个答案:

答案 0 :(得分:0)

将以下注释添加到ParentClass

@XmlSeeAlso({ChildClass.class})