java - Using custom ObjectFactory with JAXB: is there an established "go to" pattern? -
i understand objectfactory automatically generated when working jaxb 1 might define schema , xml first. however, not way can approach project.
i have existing code needs annotated , extended use jaxb use in conjunction rest service. have handful of classes , annotated them already. far understood documentation (i new jaxb), need implementation of objectfactory either package automatic invocation on package level, or multitude of implementations when referred directly rather referred package context.
i bit unsure best approach be. if use 1 implementation per package manager rather abstract, instantiating many classes. however, not sure "right" way it. opt separate concerns instantiation separate instances of objectfactory, i.e., have 1 factory per class. hence, implement similar data access object pattern.
my engineering background tells me separation of concerns , opting extension on modification better choice. hence, intuition tells me monolithic objectfactory used when produced result of approach starting xml rather code. yet don't have enough experience make informed choice.
i ask not experience technology , recommendation (which opinion based) whether approach introduce risks missing technical limitations regarding jaxb might run if pursue course of action. thank you!
creating jaxbcontext on package name
when create jaxbcontext on package name:
jaxbcontext jc = jaxbcontext.newinstance("com.example.foo"); the jaxb implementation doesn't package scanning, needs find in package can derive rest of model. can either be:
- an
objectfactoryclasscreatemethods reference domain model. bootstrapping of best when model generated xml schema - a
jaxb.indexfile, carriage return separated list of short class names (not package qualified) of classes want bootstrapjaxbcontexton. not need whole list jaxb pull in referenced classes. best use approach when start java classe.
what objectfactory used for
as far metadata concerned objectfactory has:
createmethods signature of domain model can determined (if bootstrapped onobjectfactoryalone.@xmlelementdeclannotations.@xmlelementdeclannotation@xmlrootelementannotation used in cased top-level elements have named type (see: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html).
what objectfactory not used for
the objectfactory not used during umarshal operation create instances of domain classes. more information see question linked below:
creating jaxbcontext in jax-rs environment
you mentioned doing rest. if using jax-rs implementation (such jersey) suggest using contextresolver create jaxbcontext. below example blog. in example extension in moxy jaxb impl used provide metadata, can create jaxbcontext anyway want to.
Comments
Post a Comment