简体   繁体   English

如何从XML绑定中排除属性

[英]How to exclude property from XML Binding

I am trying to marshal a class that looks like the following using JAXB 我正在尝试使用JAXB封送一个类似于以下内容的类

@javax.xml.bind.annotation.XmlType(propOrder = {"first", "last"})
public class Person
{
    private String first;
    private String last;
    public String getFirst(){
        return first;
    }
    public void setFirst(String first){
        this.first = first;
    }
    public String getLast(){
        return last;
    }
    public void setLast(String last){
        this.first = last;
    }
    public String getName(){
        return this.first + this.last;
    }
}

When I try to get the JaxbContext using: 当我尝试使用以下方法获取JaxbContext时:

JAXBContext.newInstance(Person.class);

I get the following exception: 我得到以下异常:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 182 counts of IllegalAnnotationExceptions
Property name is present but not specified in @XmlType.propOrder
this problem is related to the following location:
    at public java.lang.String myPackage.Person.getName()
    at myPackage.Person

It is very similar to the problem posted here: 这与此处发布的问题非常相似:

https://www.java.net//node/702784 https://www.java.net//node/702784

Where the solution was to add 解决方案在哪里添加

 @XmlTransient

to the offending method. 冒犯性的方法。 However, I am unable to edit the java classes to update the annotations. 但是,我无法编辑Java类来更新注释。

Any ideas how to move past this? 有什么想法可以超越这一点吗?


I ended up using a library called JAXBIntroductions which allowed me to temporarily introduce annotations at runtime, thus alleviating the problem without causing too much overhead. 我最终使用了一个名为JAXBIntroductions的库,该库使我可以在运行时临时引入注解,从而减轻了该问题,而又不会造成过多的开销。 Thanks to everyone who had a look 感谢所有看过的人


如果您无法编辑它们,为什么不创建一个新的Person子类并重写该方法以添加@XmlTransient注释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM