 |
Binding 3rd party classes witih JAXB
Posted by kohsuke on July 12, 2007 at 08:47 AM | Comments (8)
One common complaint from the JAXB users is the lack of support for binding 3rd party classes. The scenario is this — you are trying to annotate your classes with JAXB annotations to make it XML bindable, but some of the classes are coming from libraries and JDK, and thus you cannot put necessary JAXB annotations on it.
Knowing that this would be a problem, I designed the JAXB RI with a necessary hook to fix this problem. It enables programatic introduction of annotations to the JAXB runtime, so it allows people to define annotations in XML (or any other format), and make JAXB see those as if those were in the class files.
I've been asking various folks to actually implement such extension, and today I'm very happy to see that JBoss finally pulled this off with their JAXBIntroduction project.
With this project, you can write annotations in XML like this:
As you see, the structure mirrors a Java class. It's also got the wildcard support, too.
You can then create JAXBContext like this:
JaxbIntros config = IntroductionsConfigParser.parseConfig(getClass().getResourceAsStream("annotations.xml"));
IntroductionsAnnotationReader reader = new IntroductionsAnnotationReader(config);
JAXBContext jaxbContext = JAXBContext.newInstance(
new Class[] {CustomerOrder.class},
Collections.singletonMap(JAXBRIContext.ANNOTATION_READER,reader));
It doesn't support all the JAXB annotations yet, but I'm sure they'd be happy to expand the support. Also note that this extension can be used to bind the same set of classes to multiple XML representations, so this could be used for dealing with a versioning problem.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Is it work for jaxb2.0 as well, or 2.1 only?
Thanks
Posted by: maomaode on September 19, 2007 at 03:33 AM
-
Is it able to add the adapter?
The scenario is that I had the jaxb beans, and it's not annotated with the proper adapter, if i can specify the adapter class in the config file, that'll be great, means i don't need to change the existing beans.
Posted by: maomaode on September 19, 2007 at 03:40 AM
-
I don't remember if this worked with 2.0. Sorry. JBoss guys might now. And the same goes to the 2nd question — JAXB RI does let JAXBIntroduction add adapters, but I don't know if they have defined XML syntax for that.
Posted by: kohsuke on September 19, 2007 at 08:00 AM
-
I'm wondering why the JAXB API does not have some methods to omit some property to be serialized or create some alias for it... like XStream does:
http://xstream.codehaus.org/alias-tutorial.html http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/XStream.html
I think it would be so easier change the mapping settings at runtime (using the JAXB API) instead using external xmls files and download 3rd party framework (still beta)...
Posted by: leonardopinho on November 29, 2007 at 10:30 AM
-
One of the requirements to JAXB is to be able to generate a schema. This is a necessary requirement for many uses, such as web services, but at the same time this precludes a lot of things. A lot of stuff that XStream does fall into this category, unfortunately.
The other tricky aspect to this is that JAXB needs to deal with many XML APIs, including SAX, and this makes it hard to let user code participate in the process.
That said, your request is a common request, so I'm certainly in search of a solution here...
Posted by: kohsuke on December 03, 2007 at 09:16 PM
-
We are also using the RuntimeInlineAnnotationReader in the JPOX project. What's the chance of this to become part of the standard?
Posted by: ebengtso on April 15, 2008 at 02:49 AM
-
ebengtso — I don't think there's much value in having RuntimeInlineAnnotationReader in the JAXB spec or any other spec for that matter. Isn't it suffice if it's a stand-alone library?
Posted by: kohsuke on April 16, 2008 at 10:28 AM
-
Kohsuke, the problem has to do with dependencies to com.sun.* packages, and a solution that can only run in a container that uses the RI.
Posted by: ebengtso on April 22, 2008 at 01:15 AM
|