How to marshal/unmarshal Java objects with private fields using JAXB

大城市里の小女人 提交于 2019-11-30 14:30:20

You should keep your fields private in any case. You have 2 options binding to fields

1) annotate your fields with XmlElement or XmlAttribute annotation

@XmlRootElement(name="book")
public class Book {
    @XmlElement
    private String title;
    ...

2) annotate your class with @XmlAccessorType(XmlAccessType.FIELD)

    @XmlRootElement(name="book")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Book {
         private String title;
         ...

JAXB will need either: - A public instance variable Or - A private instance variable with public mutators and accessors.

You will need mutators for marshalling and acessors for unmarshalling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!