我正在尝试通过使用
我收到了以下运行时异常:
javax.xml.bind.JAXBException: com.jaxb.example.marshall.Book或其任何超类都未知于此上下文]
我的类型定义如下。
书:-
书籍清单:-
马歇尔编码:
我正在尝试放置
setBookslst()
将多个对象(例如添加到 BookLists
中的 Book
)进行编组。 我开始使用以下 JAXBContext
设置:jaxbContext = JAXBContext.newInstance(BookLists.class);
and
jaxbMarshaller.marshal(lists, result);
我收到了以下运行时异常:
javax.xml.bind.JAXBException: com.jaxb.example.marshall.Book或其任何超类都未知于此上下文]
我的类型定义如下。
书:-
@XmlRootElement(name="book")
public class Book {
private String title;
private int year;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
书籍清单:-
@XmlRootElement(name="lists")
public class BookLists {
List<Book> bookslst;
public List getBookslst() {
return bookslst;
}
public void setBookslst(List bookslst) {
this.bookslst = bookslst;
}
}
马歇尔编码:
Book book;
BookLists lists=new BookLists();
List lst=new ArrayList();
book = new Book();
book.setTitle("Book title");
book.setYear(2010);
lst.add(book);
book = new Book();
book.setTitle("Book title1");
book.setYear(2011);
lst.add(book);
lists.setBookslst(lst);
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(BookLists.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter result = new StringWriter();
jaxbMarshaller.marshal(lists, result);
String xml = result.toString();
System.out.println(xml);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我正在尝试放置
@XMLSeeAlso
注释(参考:JAXB异常:此上下文中未知的类)。我的版本中没有这个注释。