1

I am trying to convert ArrayList to xml using JAXB..

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();

... but its not working, I am getting this error:

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)...........

Please help! Thanks.


I find that this usually occurs to me when i still have a method declaration in an interface, which a class implements, but that i had later removed and had forgotten to remove it from the interface as well. I usually just save the entire solution every 30mins n then just revert back to an earlier version if i cant find the error.

4

1 回答 1

4

尝试创建一个包含您的列表并使其成为 xml 根的类,例如:

@XmlRootElement
class LDAPUsers {
    private List<LDAPUser> users;
    ... get ... set ... constructor 
}

然后封送 LDAPUsers 对象。

于 2012-08-27T10:53:32.727 回答