Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

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?

We create a JAXBContext for Order once, use a Marshaller to write the object into a StringWriter, and an Unmarshaller to read it back from a StringReader. A Java 25 project must add the Jakarta XML Binding API (jakarta.xml.bind:jakarta.xml.bind-api) and a runtime such as org.glassfish.jaxb:jaxb-runtime, because the old javax.xml.bind was deprecated in Java 9 and removed from the JDK in Java 11 by JEP 320.

Why this question is asked

XML binding is still everywhere in banking, insurance, government and SOAP integrations, and many Java developers learned it when it came free with the JDK. Interviewers ask this question to check two things at once: that we can write the basic round trip correctly, and that we know why code from older projects no longer compiles on a modern JDK. In this answer, we will recap what JAXB does, write the Order class and the round trip, set up the dependencies for Java 25, look at what happened to javax.xml.bind, and finish with the thread-safety rule that matters in real services.

1A Quick Recap of XML Binding

XML binding means converting between Java objects and XML documents automatically, guided by annotations on our classes. Turning an object into XML is called marshalling, and turning XML back into an object is called unmarshalling. The standard API for this in Java is JAXB, which today is called Jakarta XML Binding and lives in the jakarta.xml.bind package.

Three types do the work. JAXBContext is the entry point: it inspects our classes once and builds a model of how they map to XML. A Marshaller, created from the context, writes objects as XML. An Unmarshaller, also created from the context, reads XML into objects.

2Writing the Round Trip

Here is a small Order class and the code that converts it to an XML string and back. @XmlRootElement tells JAXB that Order can be the top element of a document, and @XmlAccessorType(XmlAccessType.FIELD) tells it to map the fields directly.

Java
import jakarta.xml.bind.*;
import jakarta.xml.bind.annotation.*;
import java.io.*;
import java.math.BigDecimal;
@XmlRootElement(name = "order")
@XmlAccessorType(XmlAccessType.FIELD)
public class Order {
@XmlAttribute private String id;
private String customer;
private BigDecimal total;
public Order() {} // JAXB needs a no-argument constructor
public Order(String id, String customer, BigDecimal total) {
this.id = id; this.customer = customer; this.total = total;
}
public String getCustomer() { return customer; }
}
private static final JAXBContext CTX = createContext(); // built once, reused
static JAXBContext createContext() {
try { return JAXBContext.newInstance(Order.class); }
catch (JAXBException e) { throw new IllegalStateException(e); }
}
String toXml(Order order) throws JAXBException {
Marshaller m = CTX.createMarshaller(); // cheap, one per call
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter w = new StringWriter();
m.marshal(order, w);
return w.toString();
}
Order fromXml(String xml) throws JAXBException {
Unmarshaller u = CTX.createUnmarshaller();
return (Order) u.unmarshal(new StringReader(xml));
}
String xml = toXml(new Order("A-17", "Ada", new BigDecimal("12.50")));
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// <order id="A-17">
// <customer>Ada</customer>
// <total>12.50</total>
// </order>
fromXml(xml).getCustomer(); // "Ada"

Notice that JAXB_FORMATTED_OUTPUT only adds line breaks and indentation, which is pleasant for logs and tests; without it the XML is written on one line. The cast in fromXml() is needed because unmarshal() returns Object: JAXB looks at the root element name, finds that order belongs to the Order class, and creates an instance of it.

The no-argument constructor is required because JAXB must create an empty Order before filling in its fields. It may be private or protected if we do not want other code to use it.

3Dependencies for a Java 25 Project

On a modern JDK none of these classes exist until we add them. We need two things: the API, which holds the interfaces and annotations we compile against, and an implementation, usually called the runtime, which does the actual work. The reference runtime is GlassFish JAXB; EclipseLink MOXy is an alternative. Jakarta XML Binding 4.0 belongs to Jakarta EE 10 and 11, so we use the 4.0.x line of both artifacts (check for the latest patch version).

XML
<!-- Maven -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.x</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.x</version>
<scope>runtime</scope>
</dependency>
// Gradle
implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.x")
runtimeOnly("org.glassfish.jaxb:jaxb-runtime:4.0.x")

If we forget the runtime, the code compiles but JAXBContext.newInstance() fails at startup with an exception saying that no implementation was found. In a Spring Boot project, the Boot dependency management already knows matching versions, so we can leave the version out.

4What Happened to javax.xml.bind?

From Java 6 to Java 8, JAXB was part of the JDK in the javax.xml.bind package, so it worked with no dependencies at all. Java 9 introduced modules and marked the Java EE modules, JAXB among them, as deprecated for removal. Java 11 removed them from the JDK completely, through JEP 320, together with JAX-WS, CORBA and a few others. The reasoning was that these were enterprise APIs that evolve on their own schedule and should be ordinary libraries.

Then Oracle gave Java EE to the Eclipse Foundation, where it became Jakarta EE. Because the javax name could not be used for new versions, Jakarta EE 9 renamed every package from javax.* to jakarta.*. So the history has three steps: javax.xml.bind inside the JDK, javax.xml.bind as a separate library (JAXB 2.3), and jakarta.xml.bind from version 3.0 onward. The javax versions are legacy today and get no new features.

Where JAXB has lived
PeriodPackageHow we get it
Java 6 to 8javax.xml.bindinside the JDK, no dependency
Java 9 and 10javax.xml.bindin the JDK but deprecated, needs --add-modules
Java 11 and laterjavax.xml.bindremoved (JEP 320); JAXB 2.3 library only, legacy
Jakarta EE 9 onwardjakarta.xml.bindjakarta.xml.bind-api plus a runtime (3.x, then 4.0)

5Caching the Context and Thread Safety

The code above keeps one JAXBContext in a static final field. That is deliberate. Creating a context is expensive: JAXB reflects over every class it can reach from Order, builds a mapping model, and may generate optimised accessor classes. Doing it per call wastes CPU and memory. The context is thread-safe, so one instance can serve every thread for the life of the application.

Marshaller and Unmarshaller are the opposite: they are cheap to create and not thread-safe, because they hold per-operation state such as properties, the event handler and the current position in the document. The simple rule is one context per application, and a new marshaller or unmarshaller for each operation.

6Follow-up questions

How would you migrate code from javax.xml.bind to jakarta.xml.bind?

Replace the dependencies with jakarta.xml.bind-api and jaxb-runtime 4.0.x, change every import from javax.xml.bind to jakarta.xml.bind, and regenerate any classes produced by xjc with a Jakarta-aware plugin so they carry the new annotations. Tools such as OpenRewrite have recipes that rewrite the imports automatically. Also check package-info.java files, jaxb.properties files that name a context factory, and any library that still exposes javax types in its API, because javax and jakarta annotations are different classes and are not mixed.

Can I marshal an object whose class has no @XmlRootElement?

Yes, by wrapping it in a JAXBElement that supplies the element name: m.marshal(new JAXBElement<>(new QName("order"), Order.class, order), w). This is common with classes generated by xjc from named schema types.

Why does unmarshal() not complain about unknown elements?

By default JAXB is lenient: elements it has no mapping for are skipped. To reject documents that do not match the contract, we set a Schema on the unmarshaller so it validates while it reads.

7Conclusion

In this answer, we wrote the round trip with Jakarta XML Binding: one cached JAXBContext, a Marshaller with formatted output writing to a StringWriter, and an Unmarshaller reading from a StringReader. We saw that a Java 25 project needs the jakarta.xml.bind-api artifact and a runtime such as GlassFish jaxb-runtime, that javax.xml.bind was deprecated in Java 9 and removed in Java 11 by JEP 320 before becoming jakarta.xml.bind, and that the context is expensive and thread-safe while marshallers and unmarshallers are cheap and not.

What to say in the interviewAbout 70 seconds aloud

Open with

I would annotate Order with @XmlRootElement and field access, keep a no-argument constructor, and create JAXBContext.newInstance of Order.class once, in a static final field or a Spring bean, because building the context is expensive and the context is thread-safe.

Then, if they want more

To write XML, I create a Marshaller from the context, set JAXB_FORMATTED_OUTPUT to true if I want indentation, and marshal into a StringWriter. To read it back, I create an Unmarshaller and unmarshal from a StringReader, casting the result to Order. Marshallers and unmarshallers are cheap and not thread-safe, so I create one per operation.

On Java 25 nothing of this is in the JDK: I add jakarta.xml.bind-api as the API and org.glassfish.jaxb jaxb-runtime as the implementation, both 4.0, or EclipseLink MOXy as an alternative runtime.

The history is that javax.xml.bind shipped with the JDK until Java 8, was deprecated in Java 9 and removed in Java 11 by JEP 320, and when Java EE moved to Eclipse, Jakarta EE 9 renamed it to jakarta.xml.bind. So old javax code needs its imports changed and generated classes rebuilt.

About this answer

Last updated
12 November 2026
Versions it names
Java 11, Java 25, Java 9, Jakarta EE 10
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Using Jakarta XML Binding (jakarta.xml.bind), write code that turns an Order object into...": https://scrivio-interview-library.netlify.app/java/jaxb/c3db2f761f/