RestTemplate returns data in String but not populate list nested objects

拈花ヽ惹草 提交于 2019-12-11 16:59:36

问题


I have a restTemplate trying to access data from a url and then map into a a object. All the data comes but a certain nested list is not getting populated . here is the code trying to call url.

    Resp rsp = restTemplate.getForObject("https://api.flickr.com/services/rest/?api_key=**MY_API_KEY**&method=flickr.photos.search&tags=nature",Resp.class);

Here are my model classes .

 @XmlRootElement
public class Resp {
private String stat;
private Photos photos;
setters and getters

Photo and Photos are as follows

@JsonIgnoreProperties(ignoreUnknown = true)
@XmlType
public class Photos {
private String total;
private String page;
private String pages;
private String perpage;
private List<Photo> photo;
setters and getters

Photo.java

@JsonIgnoreProperties(ignoreUnknown = true)
@XmlType
public class Photo {
private String id;
private String isfamily;
private String title;
private String ispublic;
private String owner;
private String secret;
private String server;
private String isfriend;
setters and getters

the response I should get is :

<rsp stat="ok">
<photos page="2" pages="89" perpage="10" total="881">
<photo id="2636" owner="47058503995@N01" 
    secret="a123456" server="2" title="test_04"
    ispublic="1" isfriend="0" isfamily="0" />
<photo id="2635" owner="47058503995@N01"
    secret="b123456" server="2" title="test_03"
    ispublic="0" isfriend="1" isfamily="1" />
<photo id="2633" owner="47058503995@N01"
    secret="c123456" server="2" title="test_01"
    ispublic="1" isfriend="0" isfamily="0" />
<photo id="2610" owner="12037949754@N01"
    secret="d123456" server="2" title="00_tall"
    ispublic="1" isfriend="0" isfamily="0" />
</photos>
</rsp>

But i am getting everything except list of photos.

Resp [stat=ok, photos=Photos [total=420727, page=1, pages=4208, perpage=100, 
photo=]]

When i receive it in String i get all the data

 ResponseEntity<String> response = restTemplate.getForEntity(
 "https://api.flickr.com/services/rest/? 
 api_key=**MY_API_KEY**&method=flickr.photos.search&tags=nature", 
 String.class);

this returns all the expected data but not into the list of photos.


回答1:


Try to add @JsonUnwrapped in your Photos class before the property Photo- @JsonUnwrapped private List photo; Also use @JsonProperty before each member variable in your Photo class.



来源:https://stackoverflow.com/questions/51337480/resttemplate-returns-data-in-string-but-not-populate-list-nested-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!