7.4. Migrating JAXB 1.0 applications to JAXB 2.x
While it's always possible to keep your JAXB 1.0 applications as-is and run them on the JAXB 2.x runtime, or have some parts of your application use JAXB 1.0 while others use 2.x, there are situations where you might prefer to migrate your existing applications from JAXB 1.0 to JAXB 2.x.
Common reasons why people would like to migrate includes:
- Taking advantage of the flexible, robust unmarshalling in JAXB 2.0
- Exposing the JAXB-bound objects through JAX-WS as web services
7.4.1. Typical migration work
This section outlines how the typical migration work proceeds:
- Replace the invocation of XJC 1.0.x with XJC 2.x. Both the CLI and ant task syntax are backward-compatible to make this easier.
- Re-compile your schema with XJC 2.x. This will generate a new and different set of classes and methods.
- Update your application code that deals with the generated code to use the newer classes and methods. This step is mostly straight-forward but may be time consuming, especially if your project is big.
- Remove the JAXB 1.0.x runtime from your application, and place in JAXB 2.x runtime instead. See the list for what jars were used in 1.0 and what are used in 2.0.
The classes generated from JAXB 2.x tends to be a lot more compact.
JAXB RI 2.x ships many of the same samples that JAXB RI 1.0.x shipped, except that they are updated to work with JAXB 2.0. So comparing those samples may help you better understand what this type of migration involves.
7.4.2. Other Miscellaneous Notes
- JAXB 2.x requires JavaSE 5 or above, whereas JAXB 1.0 runs on JavaSE 1.3 or above.
- The javax.xml.bind interfaces remain the same. So the part of the code that deals with those interfaces may remain the same.
- JAXB 1.0 unmarshaller was a lot more draconian to the errors in the input document than 2.0 is. In a sense, you can think of the 1.0 unmarshaller as equivalent to the 2.0 unmarshaller + validator. If your application was relying on this behaivior, you may want to enable validation in 2.0 by using Unmarshaller.setSchema.
JAXB 2.x by default creates plain Java beans, whereas in 1.0 it created separated interfaces and implementations. Many people find beans to be easier to work with, but you can use the following customization to tell JAXB 2.x to generate interfaces and implementations in a similar fashion.
<jxb:globalBindings generateValueClass="false" />
- When creating instances of the generated classes, JAXB 1.0 required that you do so through ObjectFactory, like new ObjectFactory().createAbc(). With 2.0, you can simply do new Abc(). This no longer throws JAXBExceptions, either.
- Binding customization syntax of 2.0 is backward compatible with 1.0. However, there are several customizations that were previously allowed in 1.0 but not in 2.0, such as using class customizations on model groups.