I'm trying to automatically deserialize XML formatted response using Spring's RestTemplate. I'm using Jackson's jackson-dataformat-xml
module, for which Spring Boot is set to auto-configure. I want to use JAXB annotations in the class I want to deserialize to, but it won't seem to work. Here's a sample of what I want the class to look like:
@XmlRootElement(name="Book")
public class Book {
@XmlElement(name="Title")
private String title;
@XmlElement(name="Author")
private String author;
}
This is based on the following XML sample:
<Book>
<Title>My Book</Title>
<Author>Me</Author>
</Book>
However, with class annotated like that above, the fields are always set null
. I did some experiments and found out that the deserialization works if I use Jackson's @JsonProperty
to annotate the child elements:
@XmlRootElement(name="Book")
public class Book {
@JsonProperty("Title")
private String title;
@JsonProperty("Author")
private String author;
}
It works, but somehow I feel like it's kind of awkward. Is there a way to get the JAXB annotations work like in my first example?
Jackson provides jackson-module-jaxb-annotations
module for the XML databinding to work with JAXB annotations. However, I'm not sure how to setup the ObjectMapper
being used by RestTemplate
to use this module.