There are a lot of JAXB annotations like XmlAccessorType, XMLAttribute, etc. When I take any sample XML schema file, and run xjc, it generates Java class representations of the types and elements in the schema. I also see that it generates very minimal JAXB-specific annotations, e.g. @XmlAccessorType(XmlAccessType.FIELD) at the class level. However, XmlAccessType can also have other values like NONE, PROPERTY and PUBLIC_MEMBER, and can also be specified at the field level.
Question: Is there a way to create the xsd schema or specify xjc options to make it generate the specific class and member level annotations that we need? Or is it standard practice to take the generated files, and then customize them?
Take a look at this web page: http://java.sun.com/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html There is a section speaking about the reasons for customizing JAXB bindings.
Note that if you modify annotations in Java classes generated by the XJC schema compiler, then the result may be that the mapping no longer corresponds to the original XML schema. Thus it should be done with care.
Personally, I usually work with the XML schema as the master and generate classes using XJC. If I need to change something, I modify the XML schema and regenerate the classes.
Hi. Actually, Ivan did provide an answer to the piece I was looking for, i.e. use the schema as the master and modify the schema as needed instead of customizing the generated files. Please describe exactly are you looking for - maybe give a specific example to help get it started - and I am sure you will get some suggestions from the group in response .
Well this is a little embarrassing. What I want to do is use XJB to generate JAXB annotated classes that needn't be modified by hand at all and are created with the annotations I want. Specifically, I am interested in setting XmlAccessType to NONE rather than FIELD which is the default. I am willing to use bindings customizations or even write a XJC plugin if I must but am having a difficult time finding information. In general, it appears there may be a bit of a gap between the options XJC supports verses those that are available to users hand writing classes. One example of this I am seeing is the inability to specify an adapter class for a complex type (see https://jaxb.dev.java.net/issues/show_bug.cgi?id=209) in my schema.
Unfortunately I wasn't able to find a solution. As a temporary work around (until I have time to revisit the problem) I have written a script that gets called by ant after XJC has run and performs the desired string replacements.
Evacuate the building! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards