JAXBException: class java.util.ArrayList nor any of its super class is known to this context

Lokesh Gupta

This exception occurs when you are using JAXB to marshal a Java object (collection type) to xml format. The stack trace looks like this:

Exception in thread "main" jakarta.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(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
	at jakarta.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
	at com.howtodoinjava.jaxb.examples.list.TestEmployeeMarshing.main(TestEmployeeMarshing.java:58)
Random exceptions
Random exceptions

1. Reason

The above exception occurs because JAXB always expects a @XmlRootElement annotation on the entity, it gets to marshal. This is mandatory and can not be skipped. This @XmlRootElement annotation is required to get metadata from the root element of XML marshalled from a Java object.

ArrayList class OR any java collection class does not have any JAXB annotations on it. Due to this JAXB is unable to parse any such java objects and raises this error.

2. The solution is to a Create Wrapper Class

This is a recommended approach because it gives you the flexibility to add/remove fields in the future e.g. size attribute.

@XmlRootElement(name = "employees")
@XmlAccessorType(XmlAccessType.FIELD)
class Employees {

  @XmlElement(name = "employee")
  private List<Employee> employees = new ArrayList<>();

  public List<Employee> getEmployees() {
    return employees;
  }

  public void setEmployees(List<Employee> employees) {
    this.employees = employees;
  }
}

Now you can use this class as below:

Employees employees = new Employees();

//Add the employees in list
Employee emp1 = new Employee(1, "Lokesh", "Gupta", null, null);
Employee emp2 = new Employee(1, "John", "McLean", null, null);
employees.getEmployees().add(emp1);
employees.getEmployees().add(emp2);

//Write to XML
JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

//Marshal the employees list in console
jaxbMarshaller.marshal(employees, System.out);

//Marshal the employees list in file
jaxbMarshaller.marshal(employees, new File("out.xml"));

The program output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees>
    <employee>
        <id>1</id>
        <firstName>Lokesh</firstName>
        <lastName>Gupta</lastName>
    </employee>
    <employee>
        <id>1</id>
        <firstName>John</firstName>
        <lastName>McLean</lastName>
    </employee>
</employees>

See the complete example in this post: Marshalling example java ArrayList or Set

Happy Learning !!

Comments

Subscribe
Notify of
guest
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode