How do I use JAXB annotations with Spring RestTemplate?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If a Spring client has to send or receive XML, JAXB annotations give you a clean way to map Java classes to that XML structure. RestTemplate can then serialize and deserialize those classes through an XML message converter. The key is to annotate the model correctly and make sure the template has a converter that understands JAXB-bound objects.
Annotate the Model for XML
JAXB works by reading annotations on a Java class and turning that class into XML, or the reverse. A small model class with a root element and field mappings is enough for many APIs.
@XmlRootElement defines the top-level XML element. @XmlAccessorType(XmlAccessType.FIELD) tells JAXB to bind the fields directly, which is usually simpler than annotating every getter.
Configure RestTemplate for XML
Spring needs an HTTP message converter that can turn a JAXB-annotated class into XML and parse XML back into that class. The usual choice is Jaxb2RootElementHttpMessageConverter.
In many Spring applications the converter is already present, but it is worth checking explicitly if XML mapping is failing. If the converter is missing, RestTemplate will not know how to handle the JAXB class.
Send XML Requests
Once the model and converter are in place, you can post a JAXB-annotated object just like any other request body. Set the content type to application/xml so the server sees the intended format.
The same mechanism works for put, exchange, and other RestTemplate methods. The converter handles the XML transformation.
Read XML Responses
For simple GET operations, the code is even smaller. If the server responds with XML that matches the JAXB model, RestTemplate will build the Java object directly.
If the XML shape does not match the class structure, deserialization will fail. In that case, the model annotations usually need adjustment rather than changes to the request code.
Handle Collections and Nested Elements
Real XML payloads often contain nested objects or repeated elements. JAXB supports that with @XmlElementWrapper, @XmlElement, and nested annotated classes. The important rule is that the Java structure should mirror the XML structure closely. If the XML uses wrapper elements, your model usually should as well.
It is also common to add a no-argument constructor because JAXB needs one for deserialization.
Know the Limits
RestTemplate is still widely used, but Spring now treats it as a mature synchronous client rather than the preferred path for new reactive workloads. That does not affect JAXB usage directly, but it does mean XML client code should stay simple and explicit. If the service contract is XML and synchronous, RestTemplate with JAXB is still a practical solution.
Common Pitfalls
- Forgetting
@XmlRootElementand then wondering why the converter cannot marshal the class. - Missing a no-argument constructor required for JAXB deserialization.
- Failing to register an XML message converter with
RestTemplate. - Sending XML without setting
Content-TypeandAcceptheaders appropriately. - Modeling nested XML loosely so the Java class no longer matches the payload shape.
Summary
- Use JAXB annotations to map Java classes directly to XML.
- '
RestTemplateneeds a JAXB-aware message converter to serialize and deserialize those classes.' - '
@XmlRootElementand a no-argument constructor are usually required.' - Set XML headers explicitly when making requests.
- If parsing fails, inspect the Java model first because the XML shape and class structure must match.
Related reading
- How do I use optional parameters in Java?
- How do I use reflection to invoke a private method?
- How do I use Spring Boot to serve static content located in Dropbox folder?
- How do I use toolsoverridelibrary in a build.gradle file?
- How do I write a correct micro-benchmark in Java?
- How do servlets work? Instantiation, sessions, shared variables and multithreading
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.