Using custom ObjectFactory with JAXB: is there an established "go" pattern?
I realize this is ObjectFactory
often generated automatically when working with JAXB, since the schema and XML can be defined first. However, I am unable to approach the project.
I have existing code that needs to be annotated and extended in order to use JAXB in order to use it along with a REST service. I only have a few classes and are annotated already. As far as I understood the documentation (I'm new to JAXB), I need an implementation ObjectFactory
either by a package to be called automatically at the package level, or by multiple implementations where they are passed directly and not mentioned by the package context.
I'm a little unsure what the best approach would be. If I were to use one implementation per package, then the manager would be rather abstract, instantiating many classes. However, I'm not sure if this is the "correct" way to do it. I personally would prefer to split the issues for an instance into separate instances ObjectFactory
, i.e. Have one factory for each class. Hence, I would implement something similar to the Data Access Object pattern .
My engineering background tells me that separating concerns and choosing an extension to modify would be the best choice. Hence my intuition tells me that a monolithic ObjectFactory
is only used when it is created as a result of an XML-based approach, not code. However, I don't have enough experience to make an informed choice.
I would like to ask you not only about your experience with the technology and recommendation (which will be opinion-based), but whether this approach will introduce any risks that I do not have, as well as the technical limitations regarding JAXB that I can if I continue my course of action. Thank!
source to share
Create JAXBContext
in package name
When created JAXBContext
in the package name:
JAXBContext jc = JAXBContext.newInstance("com.example.foo");
The JAXB implementation does not scan for packages, so it needs to find something in that package from which it can get the rest of the model. This could be:
- A class
ObjectFactory
with methodscreate
that reference the domain model. Bootstrapping this is best when your model is built from XML Schema - The file
jaxb.index
, which is just a caret, returns a separate list of short class names (not qualified packages) of the classes you want to load at the topJAXBContext
. It doesn't have to be the entire list, as JAXB will pull on class references. It is better to use this approach when you are starting with a Java class.
What is used ObjectFactory
for
As for the metadata, it ObjectFactory
has:
-
create
methods, from the signature of which the domain model can be determined (if you loaded only inObjectFactory
. -
@XmlElementDecl
annotations. Annotation@XmlElementDecl
is similar to annotation@XmlRootElement
, but is used in the skin for top-level elements that have a named type (see http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html ).
What is ObjectFactory
not used for
ObjectFactory
not used during operation umarshal
to instantiate domain classes. For more information see the question linked below:
Creation JAXBContext
in the JAX-RS environment
You mentioned that you are doing REST. If you are using a JAX-RS implementation for this (Jersey for example), I would suggest using ContextResolver
to create JAXBContext
. Below is an example from my blog. This example uses the MOXy JAXB impl extension to provide metadata, but you can create this one JAXBContext
anyway you want.
source to share