我计划使用jaxb在to中解锁xml。现在,这个pojo将被序列化,并通过网络发送到其他应用程序。现在,其中一些POJO包含一个int字段(4或8个字节),从xml读取该字段时,需要根据某些标志以小endian或大endian符号序列化。(此标志不是逐个字段,而是在应用程序实例级别)。在Jaxb中是否存在,而解组可以通过xml中的某种注释来实现这一功能设置呢?
在这方面的任何帮助都是非常感谢的。
谢谢
发布于 2018-04-17 15:17:37
您可以在POJO中指定自己的XmlAdapter:
@XmlElement(name="intField")
@XmlJavaTypeAdapter(EndianIntAdapter.class)
public int intField;然后实现适配器:
class EndianIntAdapter extends XmlAdapter<String, Integer> {
@Override
public String marshal(Integer field) throws Exception {
//int to string based on endianness
}
@Override
public Integer unmarshal(String str) throws Exception {
//String from xml to int based on endianness
}
}https://stackoverflow.com/questions/49881734
复制相似问题