RestTemplate以String形式返回数据,但不填充列表嵌套对象 [英] RestTemplate returns data in String but not populate list nested objects

查看:543
本文介绍了RestTemplate以String形式返回数据,但不填充列表嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个restTemplate试图从URL访问数据,然后映射到一个对象. 所有数据都来了,但是没有填充某个嵌套列表. 这是尝试调用url的代码.

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);

这是我的模型课.

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

照片和照片如下

@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

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

我应该得到的答复是:

<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=]]

当我在String中收到它时,我会获取所有数据

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.

推荐答案

尝试在属性Photo-之前在您的Photos类中添加@JsonUnwrapped @JsonUnwrapped 私人清单照片; 还可以在Photo类中的每个成员变量之前使用@JsonProperty.

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.

这篇关于RestTemplate以String形式返回数据,但不填充列表嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆