开发者

JAXB: how to tell whether a class is Marshalable without annotations

开发者 https://www.devze.com 2023-01-10 04:52 出处:网络
We have a class, with no control over the source, so no way to annotate it for JAXB.We also have a framework to take care of marshaling.Is there any way for this framework to ask that class whether it

We have a class, with no control over the source, so no way to annotate it for JAXB. We also have a framework to take care of marshaling. Is there any way for this framework to ask that class whether it is marshallable when no annotations are pre开发者_JS百科sent?


There is no standard mechanism, but I have seem people accomplish this by trying to create the JAXBContext on the class:

public boolean isValidJAXBClass(Class aClass) {
    try {
        JAXBContext.newInstance(aClass);
    } catch(JAXBException e) {
        return false;
    }
    return true;
}

You don't need any annotations to marshal a JAXB object. You can get around having to have an @XmlRootElement by wrapping it in a JAXBElement.

If you want an alternative means to represent the metadata, EclipseLink JAXB (MOXy) has a externalized binding file based on the JAXB metadata

A sample file looks something like:

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
    <java-types>
        <java-type name="org.example.order.PurchaseOrder">
            <java-attributes>
                <xml-attribute java-attribute="id"/>
                <xml-element java-attribute="customer">
                    <xml-java-type-adapter value="org.example.order.CustomerAdapter"/>
                </xml-element>
                <xml-element java-attribute="lineItems" name="line-item"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

For more information see:

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
0

精彩评论

暂无评论...
验证码 换一张
取 消