JAXB super class field not being deserialized

727 views Asked by At

I am trying to deserialize xml string into object but ran across strange problem. Everything is being serialized as intended, however when deserializing field which originates from parent class always returns null.

MyNffgDescriptor is a class which contain VNFTypeReader as attribute. All fields are deserialized correctly, VNFTypeReader ones as well except the name which is passed from parent class (NamedEntityReaderImpl) and it returns null.

Parent class

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;
    }

}

Child class

    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();
    }

}

This is the example of xml string I am trying to deserialize:

<?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>

Place where unmarshalling occurs:

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

There are 1 answers

0
martidis On BEST ANSWER

Change XmlAccessType.NONE to XmlAccessType.FIELD on parent class NamedEntityReaderImpl.

Also you could just add on class NamedEntityReaderImpl an annotation above the field:

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

Do either changes (or both) and it would work