1

I am attempting to consume an API in my Spring Boot application using an HTTP GET request which returns the below JSON. The issues I'm running into are that there is a JSON array contained inside the "playerentry" level with un-named/unheaded pairs of player and team info. For Spring, one would usually create a java class for each layer of the JSON and use the @JsonProperty() annotation to specify which part of the JSON to generate the Java Objects from. Without names for pairs contained inside the JSON array, and being unsure how to properly setup the java classes for the playerentry array and contained array pairs, I have been unable to use the RestTemplate and RestTemplateBuilder to consume this JSON. Any Help would be greatly appreciated.

{
    "rosterplayers": {
        "lastUpdatedOn": "2018-02-25 4:24:30 PM",
        "playerentry": [
            {
                "player": {
                    "ID": "10138",
                    "LastName": "Abrines",
                    "FirstName": "Alex"
                },
                "team": {
                    "ID": "96",
                    "City": "Oklahoma City",
                    "Name": "Thunder",
                    "Abbreviation": "OKL"
                }
            },
            {
                "player": {
                    "ID": "9466",
                    "LastName": "Acy",
                    "FirstName": "Quincy"
                },
                "team": {
                    "ID": "84",
                    "City": "Brooklyn",
                    "Name": "Nets",
                    "Abbreviation": "BRO"
                }
            },
            {
                "player": {
                    "ID": "9390",
                    "LastName": "Adams",
                    "FirstName": "Steven"
                },
                "team": {
                    "ID": "96",
                    "City": "Oklahoma City",
                    "Name": "Thunder",
                    "Abbreviation": "OKL"
                }
            },
            {
                "player": {
                    "ID": "9375",
                    "LastName": "Afflalo",
                    "FirstName": "Arron"
                },
                "team": {
                    "ID": "103",
                    "City": "Sacramento",
                    "Name": "Kings",
                    "Abbreviation": "SAC"
                }
            },
            {
                "player": {
                    "ID": "9357",
                    "LastName": "Ajinca",
                    "FirstName": "Alexis"
                },
                "team": {
                    "ID": "110",
                    "City": "New Orleans",
                    "Name": "Pelicans",
                    "Abbreviation": "NOP"
                }
            },
            {
                "player": {
                    "ID": "9272",
                    "LastName": "Aldrich",
                    "FirstName": "Cole"
                },
                "team": {
                    "ID": "100",
                    "City": "Minnesota",
                    "Name": "Timberwolves",
                    "Abbreviation": "MIN"
                }
            },
            {
                "player": {
                    "ID": "9480",
                    "LastName": "Aldridge",
                    "FirstName": "LaMarcus"
                },
                "team": {
                    "ID": "106",
                    "City": "San Antonio",
                    "Name": "Spurs",
                    "Abbreviation": "SAS"
                }
            },
            {
                "player": {
                    "ID": "9454",
                    "LastName": "Alexander",
                    "FirstName": "Cliff"
                },
                "team": {
                    "ID": "95",
                    "City": "Orlando",
                    "Name": "Magic",
                    "Abbreviation": "ORL"
                }
            },
            {
                "player": {
                    "ID": "9299",
                    "LastName": "Allen",
                    "FirstName": "Tony"
                },
                "team": {
                    "ID": "107",
                    "City": "Memphis",
                    "Name": "Grizzlies",
                    "Abbreviation": "MEM"
                }
            }
        ]
    }
}

1 Answer 1

5

This should work

class Roasterplayers {
    String lastUpdatedOn;
    List<PlayerEntry> playerentry;
}


class PlayerEntry {
    Player player;
    Team team;
}

class Player {

    @JsonProperty("ID")
    String id;
    @JsonProperty("LastName")
    String lastName;
    @JsonProperty("FirstName")
    String firstName;
}

class Team {
    @JsonProperty("ID")
    String id;
    @JsonProperty("City")
    String city;
    @JsonProperty("Name")
    String name;
    @JsonProperty("Abbreviation")
    String abbreviation;
}

Make sure you have Setters and Getters for each field

Sign up to request clarification or add additional context in comments.

2 Comments

I attempted this structure and the first issue I encountered is that there has to be a wrapper class around the RosterPlayers class that represents the entire JSON. After I did this I was able to access the JSON.
Please elaborate on how to consume this in spring-boot application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.