I am trying to automatically deserialize a formatted XML response using Spring RestTemplate. I am using the Jackson module jackson-dataformat-xmlfor which Spring Boot is configured to automatically configure. I want to use JAXB annotations in a class that I want to use for deserialization, but it will not work. Here is an example of what I want the class to look like this:
@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 example:
<Book>
<Title>My Book</Title>
<Author>Me</Author>
</Book>
However, if the class is annotated as described above, the fields are always set null. I did some experimentation and found out that deserialization works if I use Jackson @JsonPropertyto annotate children:
@XmlRootElement(name="Book")
public class Book {
@JsonProperty("Title")
private String title;
@JsonProperty("Author")
private String author;
}
, - , . , JAXB , ?
Jackson jackson-module-jaxb-annotations XML- JAXB. , ObjectMapper RestTemplate .