JAXB XML binding
25 questions in 8 subtopics.
Marshalling and unmarshalling
Using Jakarta XML Binding (jakarta.xml.bind), write code that turns an Order object into an XML string and back. Which dependencies does a Java 25 project need, and what happened to javax.xml.bind?A service creates JAXBContext.newInstance(Invoice.class) inside a method called for each request. Under load CPU is high and heap shows many com.sun.xml.bind classes. What is wrong and how do you fix it?You must import a 6 GB XML file of 10 million <customer> elements with JAXB. Unmarshalling the whole document runs out of memory. How would you process it?JAXB was part of the JDK, was removed in Java 11, and now lives on as Jakarta XML Binding. What does this history teach about depending on platform-bundled libraries, and how would you decide whether to keep investing in XML binding at all?JAXB annotations
Explain@XmlRootElement, @XmlElement, @XmlAttribute, @XmlTransient and @XmlAccessorType, using a Product class with id as an attribute and name as an element.You must produce XML for a partner whose schema uses namespaces, a fixed element order and optional elements. Which JAXB annotations control this, and how do you keep your domain model clean?Forty services exchange XML with banks and government systems through JAXB models, half still on javax.xml.bind. Plan the migration to Jakarta XML Binding.Root elements and JAXBElement
Unmarshalling fails with: 'jakarta.xml.bind.UnmarshalException: unexpected element (uri:"http://acme.com/order", local:"order"). Expected elements are <{}order>'. What does this mean?When does xjc generate classes without @XmlRootElement, and how do you marshal and unmarshal such types using JAXBElement?XmlAdapter
Write anXmlAdapter so that a java.time.LocalDate field is written as 2026-11-04 and read back, and show how it is applied.Your XML contract represents money as <amount currency="EUR">12.50</amount>, but your domain uses an immutable Money record without a no-argument constructor. How would you map it with JAXB?Integration code across your company converts the same partner XML into domain objects in many different ways, with adapters copied between services. How would you organise mapping code at company scale?Binding collections and maps
Map a class Order with aList<Line> lines so that the XML looks like <order><lines><line>...</line><line>...</line></lines></order>. What changes if you drop the wrapper element?JAXB cannot map Map<String, Integer> to <stock><item sku="A1" qty="5"/></stock> directly. Write the adapter approach.Partner XML files contain lists with 500,000 elements, and services map them into in-memory Lists via JAXB. Memory incidents follow. What would you change in design and standards?XSD generation and validation
Validate incoming XML against anorder.xsd while unmarshalling with JAXB, and collect all errors instead of failing on the first.A partner publishes an XSD that changes twice a year. Would you generate classes with xjc (contract first) or annotate Java classes and generate the XSD (code first)? How would you build it?Design how XML contracts with 30 external partners are governed: schema storage, versioning, generated code and testing.Your company runs 40 services with JAXB-based integrations, some generated from XSDs nobody owns. How do you decide when to retire JAXB and the XML contracts behind it?MOXy JSON binding
Using MOXy, how would you map a JSON propertyaddress.city into a flat field city on a Customer class, and what properties make the JSON output omit the root element?What is EclipseLink MOXy, and why would a team use it to produce JSON from JAXB-annotated classes instead of Jackson?A Jakarta EE application uses MOXy for JSON while new Spring Boot services use Jackson, and clients see different JSON for the same types. How would you converge?Common JAXB errors
Explain each JAXB error and its fix: (a) 'IllegalAnnotationsException: Class has two properties of the same name "id"', (b) 'Implementation of JAXB-API has not been found on module path or classpath', (c) 'does not have a no-arg default constructor'.After migrating to Spring Boot 3 and Jakarta, a SOAP client fails at startup with ClassNotFoundException: javax.xml.bind.JAXBElement, coming from a third-party library. How do you resolve it?A security scan shows that several services parse XML from partners with default parser settings. Explain the risk with JAXB and how to fix it across the company.No question matches these choices.