4

背景

我在一个名为 Attachment 的 JAR 文件中有一个类。定义的重要部分如下。

public class Attachment
{
    public List<Media> media;

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

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

我正在尝试使用JAXB-impl 2.1.3使用以下代码对其进行反序列化:

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

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

但是,这给了我以下错误(为简洁起见)

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
            ...

我知道问题在于,默认情况下,JAXB 将使用PUBLIC_MEMBER的访问类型,其定义为:

每个公共 getter/setter 对和每个公共字段都将自动绑定到 XML,除非由 XmlTransient 注释。

问题

所以,我的问题是,我如何告诉 JAXB 忽略该字段而只使用 getter/setter。请注意,我更喜欢这种方式,因为我需要忽略许多私有字段(我相信实际上附件已被错误地设置为公共)。

4

2 回答 2

2

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

于 2013-11-05T16:21:04.003 回答
2

看看Annox。它看起来有你需要的东西。

JAXB 参考实现可以配置一个特殊的注解阅读器,它可以实现不同的注解读取策略。Annox 利用了这个特性并实现了一个可以从 XML 加载 JAXB 注释的注释阅读器。

于 2013-11-05T16:00:13.990 回答