简体   繁体   English

如何使用JAXB将Java对象与私有字段一起编组/解组

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

I know the basics of the JAXB API, but I am stuck with something I am trying to do, and I am not sure whether it is actually possible. 我知道JAXB API的基础知识,但是我坚持尝试做的事情,而且不确定是否确实可行。 Details are as follows: 详细信息如下:

I have a class called Book with 2 public instance variables of type String: 我有一个名为Book的类,带有2个类型为String的公共实例变量:

@XmlRootElement(name="book")
public class Book
{
    public String title;
    public String author;

    public Book() {
    }
}

I have a another class called Bookshop with 1 public instance variable of type ArrayList: 我有一个名为Bookshop的其他类,带有1个ArrayList类型的公共实例变量:

@XmlRootElement(name="bookshop")
public class Bookshop
{
    @XmlElementWrapper(name="book_list")
    @XmlElement(name="book")
    public ArrayList<Book> bookList;

    public Bookshop() {
        this.bookList = new ArrayList<>();
    }
}

Note: package declaration and imports are removed in order to save space. 注意:为了节省空间,删除了软件包声明和导入。

These two classes work and the output XML I get is something like: 这两个类正常工作,我得到的输出XML类似于:

<bookshop>
    <book_list>
        <book>
            <title>Book 1</title>
            <author>Author 1</author>
        </book>
        <book>
            <title>Book 2</title>
            <author>Author 2</author>
        </book>
    </book_list>
</bookshop>

As far as I know, instance variables need to be declared public in order for its class to be serialisable. 据我所知,实例变量需要声明为public以便其类可序列化。 Or, instance variables can be declared private, but accessors and mutators are needed in that case. 或者,实例变量可以声明为私有,但是在这种情况下需要访问器和更改器。

I don't like declaring instance variables public; 我不喜欢将实例变量声明为公共变量; I like using accessors and mutators. 我喜欢使用存取器和更改器。 Even then, I want some of my fields to be read-only, ie, no mutator. 即使那样,我也希望我的某些字段是只读的,即没有增变器。 But JAXB seems to require both accessors and mutators for each field you want to marshal/unmarshal. 但是JAXB似乎需要对要编组/解组的每个字段都使用访问器和更改器。 I was wondering if there is any way around this? 我想知道是否有解决办法?

You should keep your fields private in any case. 在任何情况下,您都应保持字段私有。 You have 2 options binding to fields 您有2个绑定到字段的选项

1) annotate your fields with XmlElement or XmlAttribute annotation 1)使用XmlElement或XmlAttribute批注注释字段

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

2) annotate your class with @XmlAccessorType(XmlAccessType.FIELD) 2)使用@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. JAXB将需要:-一个公共实例变量,或者-一个具有公共变量和访问器的私有实例变量。

You will need mutators for marshalling and acessors for unmarshalling 您将需要mutators进行编组,需要acessor进行编组。

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

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