Search code examples
jaxbjax-wscxf

JAXB, my own domain model and suggestions


I have a considerably big POJO domain model built incrementally since 2005. We also have the XSD incrementally developed over the same time frame. Currently, I use xmlbeans in the following way to generate xml to be sent over HTTP and as part of WebService response.

  1. Generate binding code from XSD using xmlbeans
  2. Wrote lot of mappers that populate binding code from my domain POJO (also the reverse)
  3. Marshall it to XML
  4. Incase of Web Services, we use Axis 1.4 which can use the Serialization/Desrialization factories generated by xmlbeans. This allows us the have the same code to be reused across two endpoints: XML over HTTP and SOAP/HTTP

Now, we would like to start using JAXB as it is mature, standard, supports multi-tenancey (atleast the Moxy implementation) and also not much work is being done as part of xmlbeans anymore. So my questions are

  1. How to use JAXB without generating the binding code. i.e. I doesn't want to write or maintain the mapping code (Step 2) anymore. I am wondering if I can somehow use the JAXB to unmarshal to my domain POJO directly?
  2. How to integrate this with my WebService? My webservice uses the same schema and doesnt want to replicate the mappings.
  3. Any other best practice suggestions?
  4. Any Gotchas/Lessons learned I should be aware of?

Solution

  • Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    1 - How to use JAXB without generating the binding code. i.e. I doesn't want to write or maintain the mapping code (Step 2) anymore. I am wondering if I can somehow use the JAXB to unmarshal to my domain POJO directly?

    The key to using your own domain objects is path based mapping. Without this the model used by your binding framework must closely resemble the XML document. Below is a sample I like to give where a simple address object is mapped to the result of Google Geocoding API V2.

    package blog.geocode;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="kml")
    @XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
    public class Address {
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
        private String street;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
        private String city;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
        private String state;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
        private String country;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
        private String postalCode;
    
    }
    

    Below is a link to an example where the same object model is mapped to both the Google and Yahoo weather services:


    2 - How to integrate this with my WebService? My webservice uses the same schema and doesnt want to replicate the mappings.

    If Axis supports multiple JAXB providers then you should be able to leverage MOXy without much configuration. Potentially just by adding a jaxb.properties file in the same package as your domain model.


    3 - Any other best practice suggestions?

    If you want to avoid adding any compile time dependencies into your domain model, then you may want to consider representing the metadata as XML.


    UPDATE

    The following are the answers to the questions you made via comments:

    One restriction I know with the webservice API's is that they work against the "factories generated by the xjc." In which case I believe I can no longer use the XPath mapping. Please correct me if I am wrong. Also, If I use annotations, should I still need to use xjc?

    I'm not sure about CXF, but standard JAX-WS implementations allow you to start from your own domain model, or generate one via XJC. Below are a couple of examples of using MOXy in a JAX-WS environment when you start with Java objects:


    I assume this is another option other than using XPath and is also the only option if I need to support multi-tenancy.

    The external mapping document isn't an alternative to XPath, it's just another way to specify the metadata. Some people prefer this approach if they don't want to introduce any MOXy dependencies on classes that will be used for more that just object-to-XML or object-to-JSON conversions. For multi-tenancy I often annotate all the shared properties, and then have a mapping document per tenant to map the extensions.


    Also, are there any performance implications if I go the XPath route or if i chose metadata in XML?

    There aren't any performance implications of using path based mapping. This all gets optimized when the JAXBContext is initialized. When bootstrapping for an external binding document the JAXBContext creation is slightly slower as a little more work is being done, but as JAXB/MOXy is configuration by exception these documents tend to be small. There is no impact on the Marshaller/Unmarshaller of using external mapping documents.