Generate additional custom method with jaxb-xjc

青春壹個敷衍的年華 提交于 2019-12-18 05:44:01

问题


There's some way to generate a custom method within an class generated with JAXB.

I search around tutorials, including oracle's tutorial, but I didn't find clear instructions how can I custom methods to a generated class described on XML Schema.


回答1:


You can write an XJC plugin:

  • http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html



回答2:


I have found the following to be the best way to add custom behavior:

https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#compiling-xml-schema-adding-behaviors

You can implement the custom behavior for a JAXB generated class BaseType in a custom class BaseTypeExt that extends the JAXB generated class BaseType.

You also tell JAXB to use your extended class BaseTypeExt (instead of the BaseType class it extends) as the base class for all classes that normally would have extended the BaseType class.

I used this successfully to override the toString() method in my generated class BaseType.

This seems simpler than writing an XJC plugin.



来源:https://stackoverflow.com/questions/4875469/generate-additional-custom-method-with-jaxb-xjc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!