Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 强制JAXB忽略第三方类文件上的getter/setter 背景_Java_Jaxb - Fatal编程技术网

Java 强制JAXB忽略第三方类文件上的getter/setter 背景

Java 强制JAXB忽略第三方类文件上的getter/setter 背景,java,jaxb,Java,Jaxb,我在JAR文件中有一个名为Attachment的类。定义的重要部分如下 public class Attachment { public List<Media> media; public List<Media> getMedia() { return this.media; } public void setMedia(List<Media> media) { this.me

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

public class Attachment
{
    public List<Media> media;

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

    public void setMedia(List<Media> media)
    {
        this.media = media;
    }
}
然而,这给了我以下的错误(为了简洁起见被剪掉)

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

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

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

看一看。它看起来有你需要的


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

注意:我是E主管和专家组的成员

MOXy提供了一个外部映射文档扩展来支持第三方类

样本映射文档

<?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>
了解更多信息


加载时编织和必要时应用XmlTransient可能是一个想法,但这只是一个猜测,只是为了绝对清楚-这是一个我无法控制的第三方类-我无法编辑它以应用属性或类似内容。还要注意的是,第三方使用JAXB进行序列化,因此我想自己使用它。我发现Moxy在任何时候都非常棒(做得好)。我从来不知道我能做到,所以谢谢你。
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
            ...
<?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>
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);
    }

}