Quick revision
The short answer
We annotate the class with @JacksonXmlRootElement(localName = "book"), mark id with @JacksonXmlProperty(isAttribute = true), and put @JacksonXmlElementWrapper(localName = "authors") together with @JacksonXmlProperty(localName = "author") on the list. Then new XmlMapper() writes it with writeValueAsString(book) and reads it back with readValue(xml, Book.class), because XmlMapper is an ObjectMapper for XML.
What to remember
In this answer, we added jackson-dataformat-xml, mapped Book with @JacksonXmlRootElement, an attribute id through @JacksonXmlProperty(isAttribute = true), and a wrapped authors list through @JacksonXmlElementWrapper plus @JacksonXmlProperty. We wrote and read it with XmlMapper, which is an ObjectMapper in Jackson 2, saw that lists are wrapped by default and how defaultUseWrapper(false) changes that, and noted that Jackson does not do XSD validation or full namespace modelling like JAXB.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We annotate the class with @JacksonXmlRootElement(localName = "book"), mark id with @JacksonXmlProperty(isAttribute = true), and put @JacksonXmlElementWrapper(localName = "authors") together with @JacksonXmlProperty(localName = "author") on the list. Then new XmlMapper() writes it with writeValueAsString(book) and reads it back with readValue(xml, Book.class), because XmlMapper is an ObjectMapper for XML.
Background to this question
Many systems still exchange XML: partner feeds, banking formats, older SOAP-era services and configuration files. When our code base already uses Jackson for JSON, it is convenient to use the same library and the same programming model for XML. This question checks whether we know the handful of XML-specific annotations, how lists are wrapped, and where Jackson's XML support stops. In this answer, we will set up the dependency, map the Book class, write and read it, see what changes when we drop the wrapper annotation, and finish with the limits of Jackson's XML support.
1Setting Up jackson-dataformat-xml
Jackson's XML support lives in a separate module, jackson-dataformat-xml. In a Spring Boot project the version is managed for us, so we add the dependency without a version. The module provides XmlMapper and a few annotations in the com.fasterxml.jackson.dataformat.xml.annotation package for Jackson 2. Jackson 3 moves the module to new package names, so check your version when copying imports.
XmlMapper is a subclass of ObjectMapper in Jackson 2. That is the key idea of this module: everything we know from JSON, such as @JsonProperty, @JsonIgnore, serialization features and modules, works for XML too, and the XML annotations only add what JSON has no concept of, such as attributes and element wrappers.
2Mapping the Book Class
We want this XML: the id as an attribute on the book element, a title element, and the authors list inside an authors element with one author element per name.
Each annotation answers one XML question. @JacksonXmlRootElement names the root element; without it Jackson uses the simple class name, Book. @JacksonXmlProperty(isAttribute = true) turns a property into an attribute instead of a child element. For the list, @JacksonXmlElementWrapper names the outer element that wraps the items, and @JacksonXmlProperty(localName = "author") names each item element. Notice that the field is called authors while each item is called author: the two annotations let the wrapper and the items have different names.
3Writing and Reading the XML
With the mapping in place, writing and reading look exactly like JSON with an ObjectMapper. XmlMapper is thread-safe once configured, so we create it once and reuse it.
6 messages in order. Play them, or step through them one at a time.
The same annotations drive both directions.
When reading, Jackson matches the id attribute to the id property just as it matches a JSON field, and collects every author element inside authors into the list. If we also need an XML declaration at the top, the generator feature ToXmlGenerator.Feature.WRITE_XML_DECLARATION adds it.
4What Happens Without the Wrapper Annotation
Lists are wrapped by default in Jackson's XML module. If we remove both list annotations, Jackson still wraps the list, but it uses the property name for both the wrapper and each item, which is rarely what anyone wants.
setDefaultUseWrapper(false) on a JacksonXmlModule, or defaultUseWrapper(false) on the builder, makes lists unwrapped by default: repeated elements directly under the parent. Per property, @JacksonXmlElementWrapper(useWrapping = false) does the same. Many existing XML formats use repeated elements without a wrapper, so this is the setting we reach for when matching a partner's format.
5Where Jackson's XML Support Stops
Jackson maps XML to objects the way it maps JSON, which covers most data exchange. It is not a full XML binding framework. It does not validate documents against an XSD schema, so if a partner requires schema validation we run a javax.xml.validation Validator, from the JDK, before or after mapping. Namespace support is limited to setting a namespace on elements and attributes; it does not model complex namespace-qualified documents the way JAXB, now Jakarta XML Binding, does. Mixed content, text and elements interleaved, is also awkward.
So for simple data formats Jackson is a good choice and keeps one library and one model. For XSD-first contracts with heavy namespaces, JAXB is the better tool.
6Follow-up questions
Can we use a Java record for the Book with XmlMapper?
Records work with Jackson since 2.12, and the XML annotations can be placed on the record components. Some combinations, especially attributes and wrapped lists on records, have had bugs in specific versions, so I would test the round trip with the exact version in use, or use a class as shown when in doubt.
How do we write the text content of an element that also has attributes?
We mark one property with @JacksonXmlText. For example, a Price class with a currency attribute and a @JacksonXmlText BigDecimal value is written as <price currency="EUR">12.50</price>.
Does Spring MVC use XmlMapper automatically?
Yes. When jackson-dataformat-xml is on the classpath, Spring Boot registers an XML message converter that uses an XmlMapper, so controllers can produce XML when the client asks for it. That is useful, but it can also make browsers receive XML unexpectedly, so APIs should state what they produce.
7Conclusion
In this answer, we added jackson-dataformat-xml, mapped Book with @JacksonXmlRootElement, an attribute id through @JacksonXmlProperty(isAttribute = true), and a wrapped authors list through @JacksonXmlElementWrapper plus @JacksonXmlProperty. We wrote and read it with XmlMapper, which is an ObjectMapper in Jackson 2, saw that lists are wrapped by default and how defaultUseWrapper(false) changes that, and noted that Jackson does not do XSD validation or full namespace modelling like JAXB.
Open with
I would add jackson-dataformat-xml and annotate the class. On the class, JacksonXmlRootElement with localName book, so the root is lower-case book rather than the class name. On id, JacksonXmlProperty with isAttribute true, so it becomes book id equals 7.
Then, if they want more
On the authors list, two annotations: JacksonXmlElementWrapper with localName authors for the outer element, and JacksonXmlProperty with localName author for each item.
Then the code is the same as JSON: new XmlMapper, writeValueAsString of the book, and readValue of the XML with Book.class, because XmlMapper is a subclass of ObjectMapper in Jackson 2, so normal Jackson annotations and features work as well.
I would mention that lists are wrapped by default, and without my annotations the items would be called authors too, inside an authors wrapper; if a format wants repeated elements without a wrapper, I set defaultUseWrapper to false on the builder or useWrapping false on the property.
And I would point out the limits: Jackson does no XSD validation and only basic namespace support, so for schema-first contracts I would validate separately or use JAXB.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
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 Jackson's XmlMapper (jackson-dataformat-xml), map a Book class so that id is an...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/ff5ca4fb84/