JAXB超类字段未被反序列化

时间:2018-02-11 21:48:38

标签: java xml serialization jaxb

我正在尝试将xml字符串反序列化为对象,但遇到了奇怪的问题。所有内容都按预期序列化,但是当反序列化源自父类的字段时,始终返回null。

MyNffgDescriptor是一个包含VNFTypeReader作为属性的类。所有字段都被正确反序列化,VNFTypeReader也是如此,除了从父类(NamedEntityReaderImpl)传递的 name 之外它返回null。

家长班

package it.polito.dp2.NFV.sol3.client1.implementations;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import it.polito.dp2.NFV.NamedEntityReader;


@XmlAccessorType(XmlAccessType.NONE)
public class NamedEntityReaderImpl implements NamedEntityReader, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String entityName;

    public NamedEntityReaderImpl() {
        this.entityName = "";
    }

    public NamedEntityReaderImpl(String name) {
        this.entityName = name;
    }

    @Override
    public String getName() {
        return this.entityName;
    }

}

儿童课

    package it.polito.dp2.NFV.sol3.client1.implementations;

import java.io.Serializable;

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

import it.polito.dp2.NFV.VNFTypeReader;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VNFTypeReaderImpl extends NamedEntityReaderImpl implements Serializable,VNFTypeReader {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @XmlElement
    private int requiredMemory;

    @XmlElement
    private int requiredStorage;

    @XmlElement
    private it.polito.dp2.NFV.FunctionalType funcType;

    public VNFTypeReaderImpl() {
        super(null);
        this.requiredMemory = 0;
        this.requiredStorage = 0;
        this.funcType = null;
    }

    public VNFTypeReaderImpl(VNFTypeReader reader) {
        super(reader.getName());
        this.requiredMemory = reader.getRequiredMemory();
        this.requiredStorage = reader.getRequiredStorage();
        this.funcType = reader.getFunctionalType();
    }

    @Override
    public int getRequiredMemory() {
        return this.requiredMemory;
    }

    @Override
    public int getRequiredStorage() {
        return this.requiredStorage;
    }

    @Override
    public it.polito.dp2.NFV.FunctionalType getFunctionalType() {
        return this.funcType;
    }

    @Override
    @XmlElement(name="name", required=true)
    public String getName() {
        return super.getName();
    }

}

这是我尝试反序列化的xml字符串的示例:

<?xml version="1.0" encoding="UTF-8"?>
<myNffgDescriptor>
   <nodeList id="0">
      <funcReader>
         <requiredMemory>620</requiredMemory>
         <requiredStorage>100</requiredStorage>
         <funcType>WEB_SERVER</funcType>
         <name>WEBSERVERt</name>
      </funcReader>
      <hostName>H3</hostName>
      <linksList source="0" destination="10" />
      <linksList source="0" destination="11" />
      <linksList source="0" destination="8" />
   </nodeList>
</myNffgDescriptor>

发生解组的地方:

    jaxbContext = JAXBContext.newInstance(MyNffgDescriptor.class);
    unmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = new StringReader(nffg);
    MyNffgDescriptor nffgDesc = (MyNffgDescriptor) unmarshaller.unmarshal(reader);

1 个答案:

答案 0 :(得分:3)

在父级上将XmlAccessType.NONE更改为XmlAccessType.FIELD  class NamedEntityReaderImpl。

你也可以在字段上方的NamedEntityReaderImpl类上添加一个注释:

@XmlElement(name= "name")
private String entityName;

做出任何改变(或两者兼而有之)并且它会起作用