简体   繁体   English

用JAXB继承

[英]Inheritance with JAXB

I have an XSD file which is used to generate some objects which are then shared with other systems. 我有一个XSD文件,该文件用于生成一些对象,然后与其他系统共享。 I'd like to customize some of the business logic a bit in there by making some more specific implementation of these. 我想在其中自定义一些业务逻辑,方法是对它们进行一些更具体的实现。 I'm not adding new fields which need to be serialized, but more along the lines of adding setMethods which may take different format parameters. 我没有添加需要序列化的新字段,而是添加了可能带有不同格式参数的setMethods。 These methods do the work of translating the data into a form which is needed by the underlying object. 这些方法完成了将数据转换为基础对象所需的形式的工作。

I may have a field on the JAXB object which is a string, but my system gives me an integer. 我可能在JAXB对象上有一个字符串字段,但是我的系统给了我一个整数。 So, I want to handle the work of converting that in a class which extends my base class. 因此,我想处理在扩展我的基类的类中进行转换的工作。

So, is there anything special you need to do in order to get JAXB to look for XmlRootElement on a subclass of the object you are asking it to serialize? 因此,为了使JAXB在您要序列化的对象的子类上寻找XmlRootElement,您需要做些特殊的事情吗? Or must I include a @XmlRootElement attribute on my more specific class? 还是我必须在更具体的类中包含@XmlRootElement属性?

thanks! 谢谢!

Yes, @XmlRootElement / @XmlType should be enough. 是的, @XmlRootElement / @XmlType应该足够了。 But why don't you just add these methods to the generated classes? 但是,为什么不只将这些方法添加到生成的类中呢? (in case you are not regenerating them on each build, which is wrong imo) (以防您没有在每个版本上重新生成它们,这是错误的imo)

However I'd advice externalizing the conversion to other (converter) classes / methods. 但是我建议将转换外部化为其他(转换器)类/方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM