RB.
RB.

Reputation: 37192

Force JAXB to ignore getters/setters on 3rd-party class file

Background

I have a class in a JAR file called Attachment. The important parts of the definition are below.

public class Attachment
{
    public List<Media> media;

    public List<Media> getMedia()
    {
        return this.media;
    }

    public void setMedia(List<Media> media)
    {
        this.media = media;
    }
}

I am attempting to use JAXB-impl 2.1.3 to deserialize this with the following code:

JAXBContext jaxbContext = JAXBContext.newInstance(Attachment.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(text);
Attachment attachment = (Attachment) unmarshaller.unmarshal(reader);

However, this gives me the following error (snipped for brevity)

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 
1 counts of IllegalAnnotationExceptions

Class has two properties of the same name "media"
    this problem is related to the following location:
        at public java.util.List com.thirdparty.Attachment.getMedia()
            ...
    this problem is related to the following location:
        at public java.util.List com.thirdparty.Attachment.media
            ...

I understand that the issue is that, by default, JAXB will use an access type of PUBLIC_MEMBER, which is defined as:

Every public getter/setter pair and every public field will be automatically bound to XML, unless annotated by XmlTransient.

Question

So, my question is, how can I tell JAXB to ignore the field and just use the getter/setter. Note that I would prefer it this way round, as there are a number of private fields that I would need to ignore (I believe that Attachment has been set public in error in fact).

Upvotes: 4

Views: 1863

Answers (2)

bdoughan
bdoughan

Reputation: 149017

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

MOXy offers an external mapping document extension to support 3rd party classes.

Sample Mapping Document

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="blog.bindingfile">
    <xml-schema
        namespace="http://www.example.com/customer"
        element-form-default="QUALIFIED"/>
    <java-types>
        <java-type name="Customer">
            <xml-root-element/>
            <xml-type prop-order="firstName lastName address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="firstName" name="first-name"/>
                <xml-element java-attribute="lastName" name="last-name"/>
                <xml-element java-attribute="phoneNumbers" name="phone-number"/>
            </java-attributes>
        </java-type>
        <java-type name="PhoneNumber">
            <java-attributes>
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Bootstrapping from External Mapping Document

Below is an example of how you create the JAXBContext:

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "blog/bindingfile/binding.xml");
        JAXBContext jc = JAXBContext.newInstance("blog.bindingfile", Customer.class.getClassLoader() , properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(new File("src/blog/bindingfile/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

}

For More Information

Upvotes: 2

Paul Whelan
Paul Whelan

Reputation: 16809

Take a look at Annox.It looks to have what you need.

JAXB reference implementation can be configured with a special annotation reader which may implement a different strategy for reading annotations. Annox takes advantage of this feature and implements an annotation reader which can load JAXB annotations from XML.

Upvotes: 2

Related Questions