gpt4 book ai didi

java - JAXB 父类(super class)字段未反序列化

转载 作者:行者123 更新时间:2023-12-01 19:58:16 25 4
gpt4 key购买 nike

我正在尝试将 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);

最佳答案

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

您还可以在类 NamedEntityReaderImpl 字段上方添加注释:

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

进行其中一项更改(或同时进行两项更改),它就会起作用

关于java - JAXB 父类(super class)字段未反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48736825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com