使用Spring Boot获取外部API以获取空值

时间:2020-05-10 16:55:52

标签: java spring spring-boot api rest

api数据的结构我在这里输入代码

#:import Window kivy.core.window.Window
WindowManager:
    MyWindow:

<MyWindow>:
    GridLayout:
        id: my_window
        cols:6
        size_hint_y: None
        size_hint_x: None
        pos_hint:{"left":1,"top":1}
        width: Window.width

        Label:
            text:"LABEL"
            height: 35
            #size_hint:(0.16666,None)
        Label:
            height: 35
            #size_hint:(0.16666,None)
            text:"LABEL"
        Label:
            height: 35
            #size_hint:(0.16666,None)
            text:"LABEL"
        Label:
            height: 35
            #size_hint:(0.16666,None)
            text:"LABEL"
        Label:
            text:"LABEL"
            height: 35
            #size_hint:(0.16666,None)
        Label:
            text:"LABEL"
            height: 35
            #size_hint:(0.16666,None)

        TextInput:
            height: 35
            size_hint:(1,None)
        TextInput:
            height: 35
            size_hint:(1,None)
        TextInput:
            height: 35
            size_hint:(1,None)
        TextInput:
            height: 35
            size_hint:(1,None)
        Button:
            text:"+"
            on_release:
                root.do_something()

方法获取数据

{
    "Global": {
        "NewConfirmed": 112797,
        "TotalConfirmed": 5399508,
        "NewDeaths": 6154,
        "TotalDeaths": 362859,
        "NewRecovered": 53574,
        "TotalRecovered": 1374979
    },
    "Countries": [
        {
            "Country": "Afghanistan",
            "CountryCode": "AF",
            "Slug": "afghanistan",
            "NewConfirmed": 255,
            "TotalConfirmed": 4033,
            "NewDeaths": 6,
            "TotalDeaths": 115,
            "NewRecovered": 30,
            "TotalRecovered": 502,
            "Date": "2020-05-10T16:31:50Z"
        },
        {
            "Country": "Albania",
            "CountryCode": "AL",
            "Slug": "albania",
            "NewConfirmed": 6,
            "TotalConfirmed": 856,
            "NewDeaths": 0,
            "TotalDeaths": 31,
            "NewRecovered": 7,
            "TotalRecovered": 627,
            "Date": "2020-05-10T16:31:50Z"
        },
        {
            "Country": "Algeria",
            "CountryCode": "DZ",
            "Slug": "algeria",
            "NewConfirmed": 189,
            "TotalConfirmed": 5558,
            "NewDeaths": 6,
            "TotalDeaths": 494,
            "NewRecovered": 79,
            "TotalRecovered": 2546,
            "Date": "2020-05-10T16:31:50Z"
        },

POJO类:所有pojo类都具有getter和setters以及构造函数

发布

    public Post getEmployees(){
        RestTemplate rest = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        Post list =   rest.getForObject("https://api.covid19api.com/summary", Post.class);
        return list;
    }

全球

@JsonIgnoreProperties(ignoreUnknown = true)
public class Post {

    private Global global;

    private Country[] countries;




}

国家

@JsonIgnoreProperties(ignoreUnknown = true)
public class Global {

    private Long newconfirmed;
    private Long totalconfirmed;
    private Long newdeaths;
    private Long totaldeaths;
    private Long newrecovered;
    private Long totalrecovered;

控制器

@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {

    private String country;
    private String countryCode;
    private String slug;
    private Long newconfirmed;
    private Long totalconfirmed;
    private Long newdeaths;
    private Long totaldeaths;
    private Long newrecovered;
    private Long totalrecovered;
    private String date;

我得到的输出

    @GetMapping(produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
    public Post getAliens() {
        return repo.getEmployees();
    }

0 个答案:

没有答案