首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未反序列化的JAXB超类字段

未反序列化的JAXB超类字段
EN

Stack Overflow用户
提问于 2018-02-11 21:48:38
回答 1查看 596关注 0票数 0

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

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

父类

代码语言:javascript
复制
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;
    }

}

儿童班

代码语言:javascript
复制
    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字符串的示例:

代码语言:javascript
复制
<?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>

进行解编组的地方:

代码语言:javascript
复制
    jaxbContext = JAXBContext.newInstance(MyNffgDescriptor.class);
    unmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = new StringReader(nffg);
    MyNffgDescriptor nffgDesc = (MyNffgDescriptor) unmarshaller.unmarshal(reader);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-12 08:07:43

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

此外,您也可以在类NamedEntityReaderImpl上添加一个注释到字段的上方:

代码语言:javascript
复制
@XmlElement(name= "name")
private String entityName;

做任何更改(或者两者都做),这样就可以了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48736825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档