restTemplate.getForEntity()映射状态代码为200,但无法设置值

时间:2019-05-20 21:18:48

标签: spring mapping resttemplate

对Sports Api提供程序进行Get调用,然后使用org.springframework.web.client.RestTemplate调用方法getForEntity

交换后,我得到200状态代码和666项回复。

问题是,它们都设置为null或0。

他们发送的Json首先有字段大写。例如:"TeamID": 1

我尝试将班级字段设置为teamIDTeamID。没运气 我还尝试使用映射对象的列表制作包装响应类。


public class SportsDataTeam {

    private Integer teamID; // changed from float to Integer
    private String school;
    private String name;
    private String teamLogoUrl;
    private String shortDisplayName;

   //with standard generated getters and setters below example 

}



//Then in my service class, I call:

 ResponseEntity<SportsDataTeam[]> response = restTemplate.getForEntity(
"https://api.sportsdata.io/v3/cbb/scores/json/teams?key=df57fbe761424db78e5c16a103ceb0d0",
                SportsDataTeam[].class);



以下是请求中的前两项: 它的长度为666,但为了节省空间,仅数组的前两个

[
{
"TeamID": 1,
"Key": "SMU",
"Active": true,
"School": "SMU",
"Name": "Mustangs",
"ApRank": null,
"Wins": 15,
"Losses": 17,
"ConferenceWins": 6,
"ConferenceLosses": 12,
"GlobalTeamID": 60000001,
"ConferenceID": 1,
"Conference": "American Athletic",
"TeamLogoUrl": "https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/1.png",
"ShortDisplayName": "SMU",
"Stadium": {
"StadiumID": 101,
"Active": true,
"Name": "Moody Coliseum",
"Address": null,
"City": "Dallas",
"State": "TX",
"Zip": null,
"Country": null,
"Capacity": 7000
}
},
{
"TeamID": 2,
"Key": "TEMPL",
"Active": true,
"School": "Temple",
"Name": "Owls",
"ApRank": null,
"Wins": 23,
"Losses": 10,
"ConferenceWins": 13,
"ConferenceLosses": 5,
"GlobalTeamID": 60000002,
"ConferenceID": 1,
"Conference": "American Athletic",
"TeamLogoUrl": "https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/2.png",
"ShortDisplayName": "TEMPLE",
"Stadium": {
"StadiumID": 45,
"Active": true,
"Name": "Liacouras Center",
"Address": null,
"City": "Philadelphia",
"State": "PA",
"Zip": null,
"Country": null,
"Capacity": 10200
}
}
]

1 个答案:

答案 0 :(得分:1)

你好Braden Borman

如果您的JSON中的键位于CAPS中,例如TeamID而不是teamId,那么您需要向属性中添加@JsonProperty注释。

更新SportsDataTeam.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class SportsDataTeam {

    @JsonProperty("TeamID")
    private Integer teamId;
    @JsonProperty("Key")
    private String key;
    @JsonProperty("Active")
    private Boolean active;
    @JsonProperty("School")
    private String school;
    @JsonProperty("Name")
    private String name;
    @JsonProperty("ApRank")
    private String apRank;
    @JsonProperty("Wins")
    private Integer wins;
    @JsonProperty("Losses")
    private Integer losses;
    @JsonProperty("ConferenceWins")
    private Integer conferenceWins;
    @JsonProperty("ConferenceLosses")
    private Integer conferenceLosses;
    @JsonProperty("GlobalTeamID")
    private Long globalTeamId;
    @JsonProperty("ConferenceID")
    private Integer conferenceId;
    @JsonProperty("Conference")
    private String conference;
    @JsonProperty("TeamLogoUrl")
    private String teamLogoUrl;
    @JsonProperty("ShortDisplayName")
    private String shortDisplayName;
    @JsonProperty("Stadium")
    private Stadium stadium;

    // getters and setters

}

更新Stadium.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Stadium {

    @JsonProperty("StadiumID")
    private Integer stadiumId;
    @JsonProperty("Active")
    private Boolean active;
    @JsonProperty("Name")
    private String name;
    @JsonProperty("Address")
    private String address;
    @JsonProperty("City")
    private String city;
    @JsonProperty("State")
    private String state;
    @JsonProperty("Zip")
    private Integer zip;
    @JsonProperty("Country")
    private String country;
    @JsonProperty("Capacity")
    private Integer capacity;

    // getters & setters

}

希望这会有所帮助。