开发者

Method collision between JAXB and rest of the Application

开发者 https://www.devze.com 2023-03-24 09:39 出处:网络
I am converting an existing POJO to be an JAXB compliant. Everything works fine except for one of the getter method of the pojo where I need an additional logic specific to rendering 开发者_StackOverf

I am converting an existing POJO to be an JAXB compliant. Everything works fine except for one of the getter method of the pojo where I need an additional logic specific to rendering 开发者_StackOverflow社区XML. However, this getter is already called somewhere within the application and I cannot modify this method's behavior. How do I normally deal with such method name collisions? Is there a way I create a separate method just for JAXB purpose adn mark the currentmethod as XMlTransient?

Thanks


Yes, exactly what you said would work. Make the one method @XmlTransient, then write another method and make it an @XmlElement(name="whatever element name").


You can put XmlAccessorType(XmlAccessType.FIELD) on the class. Then the JAXB annotations will be picked up from the field names, and not the method names. For example:

@XmlAccessorType(XmlAccessType.FIELD)
public class MyType {
    @XmlElement String f1;
    @XmlElement Integer f2;

    // JAXB doesn't care about these:
    public String getF1() {return f1;}
    public String getF2() {return f2;}
    public void setF1(String f1) {this.f1 = f1;}
    public void setF2(Integer f2) {this.f2 = f2;}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号