简体   繁体   中英

How to map Riot Api request to an Object with Spring RestTemplate

I am using Spring's RestTemplate to convert a JSON response from the RiotAPI into my BasicSummoner object. I believe the issue is with converting the JSON response into my object. After calling getForObject() all of the object's fields are null/empty. Any help is appreciated as this is my first Spring project and first time using Riot's API.

I have verified that the JSON resonse is correct and looks like this:

 { "riotschmick": { "id": 585897, "name": "RiotSchmick", "profileIconId": 782, "summonerLevel": 30, "revisionDate": 1469155559000 } } 

My request looks like this:

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

And the object BasicSummoner looks like this:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true) public class 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;

}

}

Your JSON is not a single Object, but an Object inside another Object.
This means that to use your code as it is now, you need to unwrap the inner Object, or change the structure to something else.
The response seems to fit a Map<String, BasicSummoner>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM