I am using Jackson XML mapper to deserialize XML to POJO. The XML looks like
<person>
<agency>
<phone>111-111-1111</phone>
</agency>
</person>
And my class looks like
class Person
{
@JacksonXmlProperty(localName="agency", namespace="namespace")
private Agency agency;
//getter and setter
}
class Agency
{
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName="phone", namespace="namespace")
private List<AgencyPhone> phones;
//getter and setter
}
class AgencyPhone
{
private Phone phone;
//getter and setter
}
class Phone
{
private String number;
//getter and setter
}
I want to set the phone number to number in Phone class. I cannot change XML or the way the class has been structured. I am getting Cannot construct instance of resolved.agency.AgencyPhone
error and I created a AgencyPhone constructor
class AgencyPhone{
{
private Phone phone;
public AgencyPhone(Phone phone)
{
this.phone = phone;
}
}
But that did not work. So how to deserialize to nested instances.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…