restTemplate.getForEntity () сопоставляется с кодом состояния 200, но не может установить значения - PullRequest
0 голосов
/ 21 мая 2019

Выполнение вызова Get для провайдера Sports Api, и я вызываю метод getForEntity с org.springframework.web.client.RestTemplate

После обмена я получаю код состояния 200 и ответ с 666 предметами.

Проблема в том, что все они равны нулю или 0.

Json, которого они посылают, имеет начальную букву поля. пример: "TeamID": 1,

Я пытался установить поля моего класса как teamID и TeamID. Неудачно Я также попытался создать класс ответа оболочки со списком сопоставленного объекта.


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 Ответ

1 голос
/ 21 мая 2019

Привет Брэйд Борман

Если ваш 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

}

Надеюсь, это поможет.

...