XML deserialize to POJO for list values not mapping

14 views
Skip to first unread message

Aswathy Nair

unread,
Apr 3, 2021, 11:44:23 AM4/3/21
to jackson-user
Hi 

I am trying to do xml to pojo deserialization.
My xml looks like below

<?xml version="1.0" encoding="UTF-8"?>

<Photo id="xyq3994">
    <Sizes>
         <Size id="sampe1" url="http://www.sampesite.com"/>
         <Size id="sample2" url="http://sampesite.com"/>
    </Sizes>
</Photo>
</DataApi>


I have written pojo classes for the same


@Getter
@Setter
public class DataApi {

private Photo photo;
}

@Getter
@Setter
public class Photo {

private Sizes sizes;
private String id;
}

@Getter
@Setter

public class Sizes {

private List<Size> size;
}

@Getter
@Setter
public class Size {

private String id;
private String url;
}


My mapper looks like below:

public static Object getResponseXML(String responseString, Class responseClass) throws IOException {

ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return xmlMapper.readValue(responseString, responseClass);
}


@Test
public void test() {
DataApi dataApi= (DataApi) getResponseXML(response.asString(), DataApi.class);

dataApi. getPhoto().getSizes().getSize().get(0);  
}

I am getting IndexOutOfBoundException. And the list is not mapped to size on deserialization.

Can someone pls guide me what am I missing here.

I am using 2.12.2 version  com.fasterxml.jackson


Regards,

Aswathy
Reply all
Reply to author
Forward
0 new messages