【问题标题】:Overriding @XmlTransient behavior using external mapping file in Moxy在 Moxy 中使用外部映射文件覆盖 @XmlTransient 行为
【发布时间】:2012-08-16 16:51:05
【问题描述】:

为什么我不能使用如下所示的外部映射将“类型”属性覆盖为非瞬态?当我序列化时,我没有看到“类型”元素。

public class PhoneNumber
{
    private String type;

    @XmlTransient
    public String getType()
    {
        return type;
    }

    //other properties
}

我使用“xml-attribute”指定了“类型”,希望这将优先于注释,但它不起作用。

<java-type name="PhoneNumber">
         <java-attributes>
            <xml-attribute java-attribute="type" />
            <xml-value java-attribute="number" />
         </java-attributes>
</java-type>

【问题讨论】:

    标签: jaxb2 moxy


    【解决方案1】:

    您似乎遇到了错误。您可以使用以下链接跟踪我们在此问题上的进展:

    解决方法

    您可以指定应为PhoneNumber 类使用字段访问权限。

        <java-type name="PhoneNumber" xml-accessor-type="FIELD">
            <java-attributes>
                <xml-attribute java-attribute="type" />
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    

    完整示例

    电话号码

    package forum11991936;
    
    import javax.xml.bind.annotation.XmlTransient;
    
    public class PhoneNumber {
        private String type;
        private String number;
    
        @XmlTransient
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getNumber() {
            return number;
        }
        public void setNumber(String number) {
            this.number = number;
        }
    
    }
    

    oxm.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum11991936">
        <java-types>
            <java-type name="PhoneNumber" xml-accessor-type="FIELD">
                <xml-root-element name="phone-number"/>
                <java-attributes>
                    <xml-attribute java-attribute="type" />
                    <xml-value java-attribute="number"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    jaxb.properties

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    演示

    package forum11991936;
    
    import java.util.*;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11991936/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {PhoneNumber.class}, properties);
    
            PhoneNumber pn = new PhoneNumber();
            pn.setType("cell");
            pn.setNumber("555-1111");
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(pn, System.out);
        }
    }
    

    输出

    <?xml version="1.0" encoding="UTF-8"?>
    <phone-number type="cell">555-1111</phone-number>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2018-09-01
    • 1970-01-01
    • 2012-04-14
    • 2019-10-04
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多