Spring getForEntity()映射状态代码为200,但无法设置值
对体育Api提供者进行Get调用,然后使用org.springframework.web.client.RestTemplate调用getForEntity方法 交换之后,我得到了200个状态码和666个项目的回复 问题是,它们都设置为null或0 他们发送的Json以字段开头。示例:TeamID:1 我尝试将我的类字段设置为teamID和teamID。不走运 我还尝试使用映射对象的列表创建一个包装器响应类 公营体育队{ 私有整数teamID;//从浮点更改为整数 私立弦乐学校; 私有字符串名称; 私有字符串; 私有字符串shortDisplayName; //使用下面示例中的标准生成的getter和setter } //然后在我的服务课上,我呼叫: ResponseEntity response=restemplate.getForEntity https://api.sportsdata.io/v3/cbb/scores/json/teams?key=df57fbe761424db78e5c16a103ceb0d0, SportsDataTeam[]类; 以下是请求中的前两项: 它的长度是666,但为了节省空间,只需要阵列的前两个Spring getForEntity()映射状态代码为200,但无法设置值,spring,mapping,resttemplate,Spring,Mapping,Resttemplate,对体育Api提供者进行Get调用,然后使用org.springframework.web.client.RestTemplate调用getForEntity方法 交换之后,我得到了200个状态码和666个项目的回复 问题是,它们都设置为null或0 他们发送的Json以字段开头。示例:TeamID:1 我尝试将我的类字段设置为teamID和teamID。不走运 我还尝试使用映射对象的列表创建一个包装器响应类 公营体育队{ 私有整数teamID;//从浮点更改为整数 私立弦乐学校; 私有字符串名称
[
{
"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
}
}
]
你好
如果您的JSON包含的键是像TeamID而不是TeamID这样的大写键,那么您需要将@JsonProperty注释添加到属性中
更新SportsDataTeam.java
Stadium.java的更新
希望这有帮助。另外,我删除了最后一个}后面的逗号,这样这个示例就有效了。如果有帮助,您可以复制该URL并查看响应!感谢您抽出时间和提供的意见!我很快就会试一试的
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
}
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
}