要编组的元素必须是公共的,或具有XMLElement注释。ArrayList类和您的类Books不符合任何这些规则。您必须定义一种方法来提供Book值并对其进行注释。
在您的代码上,仅更改您的Books类,并添加“ self getter”方法:
@XmlRootElement@XmlSeeAlso({Book.class})public class Books extends ArrayList<Book> { public Books() { this.add(new Book("The Sign of the Four")); } @XmlElement(name = "book") public List<Book> getBooks() { return this; }}当您运行编组代码时,您将获得:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><books><book><title>The Sign of the Four</title></book></books>
(为清晰起见,我添加了换行符)



