我正在尝试使用 JAXB 将 ArrayList 转换为 xml..
ArrayList<LDAPUser> myList = new ArrayList<LDAPUser>();
myList = retrieveUserAttributes.getUserBasicAttributes(lastName,
retrieveUserAttributes.getLdapContext());
JAXBContext jaxbContext = JAXBContext.newInstance(LDAPUser.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(myList, sw);
System.out.println(sw.toString());
return sw.toString();
... 但它不起作用,我收到此错误:
27-Aug-2012 10:43:58 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/Spring3-LDAP-WebService] threw exception [Request processing failed; nested exception is javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.] with root cause javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:470) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96) at ie.revenue.spring.RestController.searchLdapUsersByLastNameTwo(RestController.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)...........
求助! 谢谢。
最佳答案
尝试创建一个包装列表的类并使其成为 xml 根,例如:
@XmlRootElement
class LDAPUsers {
private List<LDAPUser> users;
... get ... set ... constructor
}
然后编码 LDAPUsers 对象。
关于java - 如何使用 JAXB 将 ArrayList<Object> 转换为 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12139996/