0
映射防暴API請求的對象,我使用Spring的RestTemplate轉換從RiotAPI JSON響應到我BasicSummoner對象。我相信問題是將JSON響應轉換爲我的對象。調用getForObject()後,所有對象的字段都爲空/空。任何幫助表示讚賞,因爲這是我的第一個Spring項目,並且第一次使用Riot的API。如何與Spring RestTemplate
我已經驗證JSON resonse是正確的,看起來像這樣:
{
"riotschmick": {
"id": 585897,
"name": "RiotSchmick",
"profileIconId": 782,
"summonerLevel": 30,
"revisionDate": 1469155559000
}
}
我的要求是這樣的:
public BasicSummoner requestBasicSummoner() {
RestTemplate template = new RestTemplate();
String mes = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick?api_key=<my-api-key>";
BasicSummoner summoner = template.getForObject(mes, BasicSummoner.class);
log.info(summoner.toString());
return summoner;
}
和對象BasicSummoner樣子這個:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
個
@JsonIgnoreProperties(ignoreUnknown =真) 公共類BasicSummoner {
private long id;
private String name;
private int profileIconId;
private long revisionDate;
private long summonerLevel;
public BasicSummoner() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProfileIconId() {
return profileIconId;
}
public void setProfileIconId(int profileIconId) {
this.profileIconId = profileIconId;
}
public long getRevisionDate() {
return revisionDate;
}
public void setRevisionDate(long revisionDate) {
this.revisionDate = revisionDate;
}
public long getSummonerLevel() {
return summonerLevel;
}
public void setSummonerLevel(long summonerLevel) {
this.summonerLevel = summonerLevel;
}
@Override
public String toString() {
return "id=" + id + ", name=" + name + " , summoner level=" + summonerLevel;
}
}