Search code examples
springmappingresttemplate

restTemplate.getForEntity() maps with status code 200 but fails to set values


Making a Get call to a Sports Api provider and I call the method, getForEntity with a org.springframework.web.client.RestTemplate

After the exchange, I get a 200 status code and a response with 666 items.

Problem is, they are all set to null or 0.

The Json they send has fields capital to begin with. example: "TeamID": 1,

I've tried to set my class fields as teamID and TeamID. No luck I've also tried to make a wrapper response class with a list of the mapped object.


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



Here are the first two items in the request: Its a length of 666 -- but to save space, only the first two of the array

[
{
"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
}
}
]


Solution

  • Hello Braden Borman

    If your JSON has keys are in CAPS like TeamID instead of teamId, then you need to add @JsonProperty annotation to your attributes.

    Update of 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
    
    }
    

    Update of 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
    
    }
    

    Hope this helps.