gpt4 book ai didi

java - JAXB 和用于映射到 HashMap 内的基本类型的 XmlAdapter

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:30 25 4
gpt4 key购买 nike

我正在开发一些 JavaSE GUI 应用程序,它必须在 XML 文件中存储和加载数据(此处提供源代码: https://github.com/SP8EBC/MKS_JG )

在数据结构的某个地方,我有一个 HashMap,它将基本类型绑定(bind)到 Short。实际上,这个基类型是一个抽象类,因此我的软件添加子类的对象作为键。为了编码和解码这些类型,我开发了一个适配器,但不幸的是,无论我对注释做什么,这个适配器都从未使用过,因此输出 XML 文件是错误的。我在方法中放置了一些断点,并在调试软件上在这些断点处更新了停止点。

我的问题是:我应该在代码中做什么才能使用我的适配器正确处理此 HashMap?

具有基类的HashMap所在的类是这样的:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Competition {
// some other fields and methods
@XmlElement
public HashMap<LugerCompetitor, Short> startList;
}

此时基类的声明如下所示

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlJavaTypeAdapter(value = LugerCompetitorAdapter.class)
public abstract class LugerCompetitor {
/**
* .... some commentary in polish
*/
public abstract CompetitionTypes getCompetitorType();

public abstract void setStartNumber(short num);
@XmlTransient
public abstract short getStartNumber();

public abstract String toString();
}

其中一个子类看起来像这样

public class LugerDouble extends LugerCompetitor {

/**
* Saneczkarz na górze
*/
public Luger upper;

/**
* Saneczkarz na dole
*/
public Luger lower;

short startNum;

@Override
public CompetitionTypes getCompetitorType() {
// TODO Auto-generated method stub
return null;
}

public String toString() {
String out;

out = upper.surname + " / " + lower.surname;

return out;
}

@Override
public short getStartNumber() {
return this.startNum;
}

@Override
public void setStartNumber(short num) {
this.startNum = num;

}

}

如您所见,我编写了一个适配器,它应该将完全不同的子对象转换为 XML 文件中标准化的对象。适配器如下所示:

public class LugerCompetitorAdapter extends XmlAdapter<LugerCompetitorAdapter.AdaptedCompetitorLuger, LugerCompetitor> {

public static class AdaptedCompetitorLuger {

@XmlAttribute
public long lugerSystemId; // LugerSingle

@XmlAttribute
public long bottomLugerSystemId; // Dwójka sankowa - dół

@XmlAttribute
public long upperLugerSystemId; // Dwójka sankowa - góra

@XmlAttribute
public long maleLugerSystemId; // Sztafeta albo drużyna

@XmlAttribute
public long femaleSystemId; // jw

@XmlAttribute
public long doubleSystemId; // jw


}

@Override
public AdaptedCompetitorLuger marshal(LugerCompetitor v) throws Exception {

AdaptedCompetitorLuger adaptedCompetitorLuger = new AdaptedCompetitorLuger();

if (v instanceof LugerSingle) {
adaptedCompetitorLuger.lugerSystemId = ((LugerSingle)v).single.getSystemId();
return adaptedCompetitorLuger;
}
if (v instanceof LugerDouble) {
adaptedCompetitorLuger.bottomLugerSystemId = ((LugerDouble)v).lower.getSystemId();
adaptedCompetitorLuger.upperLugerSystemId = ((LugerDouble)v).upper.getSystemId();
return adaptedCompetitorLuger;


}

return null;
}

@Override
public LugerCompetitor unmarshal(AdaptedCompetitorLuger v) throws Exception {
return null;
}

}

编辑(已添加解决方案)

所以我用另一种方式做了我想要的东西。我只是为整个 HashMap 制作了一个适配器,而不是尝试对 LugerCompetitor 本身进行编码和取消编码。我的解决方案如下,当我生成 XML 时它似乎有效。相反的方向仍然需要发展。

@Component
public class StartListAdapter extends XmlAdapter<StartListAdapter.AdaptedStartList, Map<LugerCompetitor, Short>> {

RTE_ST rte_st;

@Autowired
@Lazy
public void setRTE(RTE_ST rte) {
rte_st = rte;
}

public static class AdaptedStartList {
@XmlElement(name="startListEntry")
public List<AdaptedEntry> adaptedList = new ArrayList<AdaptedEntry>();
}

public static class AdaptedEntry {

@XmlElement(required = false, nillable = true )
public Long lugerSystemId; // konkurencja pojedyncza K albo M

@XmlElement(required = false, nillable = true )
public Long lowerLugerSystemId; // dwójki sankowe - sankarz na dole

@XmlElement(required = false, nillable = true )
public Long upperLugerSystemId; // j/w ale sankarz na górze

@XmlElement(required = false, nillable = true )
public Long maleLugerSystemId; // M podczas sztafety albo konkurencji drużynowej

@XmlElement(required = false, nillable = true )
public Long femaleLugerSystemId; // K j/w

@XmlElement(required = true)
public short startNumber;
}

@Override
public AdaptedStartList marshal(Map<LugerCompetitor, Short> arg0) throws Exception {
AdaptedStartList out = new AdaptedStartList();

for (Entry<LugerCompetitor, Short> e : arg0.entrySet()) {
AdaptedEntry adaptedEntry = new AdaptedEntry();

LugerCompetitor k = e.getKey();

if (k instanceof LugerSingle) {
adaptedEntry.lugerSystemId = ((LugerSingle)k).single.getSystemId();
adaptedEntry.startNumber = e.getValue();
}
else if (k instanceof LugerDouble) {
adaptedEntry.lowerLugerSystemId = ((LugerDouble)k).lower.getSystemId();
adaptedEntry.upperLugerSystemId = ((LugerDouble)k).upper.getSystemId();
adaptedEntry.startNumber = e.getValue();

}

out.adaptedList.add(adaptedEntry);
}

return out;
}

@Override
public Map<LugerCompetitor, Short> unmarshal(AdaptedStartList arg0) throws Exception {
return null;
}

}

最佳答案

首先,如果可能但没有必要,将 LugerCompetitor 转换为接口(interface)。您可以避免所有公共(public)抽象困惑:

public interface LugerCompetitor {
/**
* .... some commentary in polish
*/
CompetitionTypes getCompetitorType();

void setStartNumber(short num);
@XmlTransient
short getStartNumber();
}

然后你可以定义一个元素来表示 LugerCompetitor 和 Short 值:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LugerCompetitorValue {
@XmlElement
@XmlJavaTypeAdapter(LugerCompetitorAdapter.class)
public LugerCompetitor competitor;

@XmlElement
public Short value;

}

并将 LugerCompetitorValue 集合添加到您的类中:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Competition {
// some other fields and methods
@XmlElement
public List<LugetCompetitorValue> lugerCompetitorValues;
}

然后适配器应该开始工作。然而,问题是您希望如何在客户端解码数据?

关于java - JAXB 和用于映射到 HashMap 内的基本类型的 XmlAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825532/

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