Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

java - Generate additional custom method with jaxb-xjc

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share

755k questions

754k answers

5 comments

53.3k users

...