簡體   English   中英

從 restTemplate 響應中讀取另一個 object 中的列表

[英]Reading a list in another object from a restTemplate response

我已經瀏覽了這里的一些問題以及他們的答案,比如這個告訴我們如何從 restTemplate 響應中提取對象列表。 但是,它並不能解決我的問題,即我有一個條目 class

@JsonIgnoreProperties(ignoreUnknown = true)
public class Entry {


    private String API;
    private String Description;
    private String Auth;
    private boolean HTTPS;
    private String Cors;
    private String Link;
    private String Category;
//    getters and setters
}

然后我有一個入口實現 class

@JsonIgnoreProperties(ignoreUnknown = true)
public class EntryImpl {
    private Long count;
    private ArrayList<Entry> entries;


    public EntryImpl () {

    }
//  getters and setters
}

這是我使用此api的請求實現

public class RestConsump {
    public static void main (String [] args) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.publicapis.org/entries";
        EntryImpl entries = restTemplate.getForObject(url, EntryImpl.class);
        System.out.println(entries.getEntries().get(0)); // returns null for all entries 
        System.out.println(entries.getCount()); // prints the numbers of entries
    }
}

我的問題是,如何實現它以便 EntryImpl 返回條目列表和計數。

我執行了上面的代碼並得到了這個響應:

{API=AdoptAPet, Description=Resource to help get pets adopted, Auth=apiKey, HTTPS=true, Cors=yes, Link=https://www.adoptapet.com/public/apis/pet_list.html, Category=Animals}
1417

我使用 spring-web:5.3.8 作為 RestTemplate,java 版本 15。也許您使用的是過時的 RestTemplate?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM