Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General question on JAXB annotations generated when using xjc

 
R Srini
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?

Thanks in advance!

Srini
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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.
Best wishes!
 
Timo Taye
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I agree the link provided is an excellent source of info on generating customized JAXB bindings, I don't think it addresses the question posed. Does anyone have a solution to this?
 
R Srini
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Timo Taye
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R Srini
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specifically, I am interested in setting XmlAccessType to NONE rather than FIELD which is the default.

Hi. Were you able to figure this out? If yes, kindly share your solution. Thank you!
 
Timo Taye
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic