Generate additional custom method with jaxb-xjc
Solution 1
You can write an XJC plugin:
Solution 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.
Related videos on Youtube
Andre Pastore
Computer Scientist Distributed Systems Architect & Engineer Software Engineer python, java, javascript, perl, bash, ... developer former-CTO Event Driven mindset Functional teams seeker Looking for diverse and multicultural leadership
Updated on May 18, 2022Comments
-
Andre Pastore almost 2 years
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.
-
Andre Pastore about 13 yearsExcellent! I think that it's exactly what I am looking for ... I'll try it and comment here about results! Thanks!
-
Andre Pastore about 13 yearsI used an existent implementation of Code Injection, exactly the same described on article. It worked as I expected. Thanks!
-
ulab almost 8 yearsthe article seems to be old. Is there any new advanced ways to do the same ?