简体   繁体   中英

How to exclude property from XML Binding

I am trying to marshal a class that looks like the following using 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.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

Where the solution was to add

 @XmlTransient

to the offending method. However, I am unable to edit the java classes to update the annotations.

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. Thanks to everyone who had a look


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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