Home > Mobile >  How to map JSON array to Java class in Spring Boot
How to map JSON array to Java class in Spring Boot

Time:02-01

I'm trying to get responce from another API convert it to Java clases and then sent to the front end using Spring Boot I have JSON response from external API like this

{
    "status": {
        "timestamp": "2023-01-31T14:06:45.210Z",
        "error_code": 0,
        "error_message": null,
    },
    "data": [
        {
            "id": 7982,
            "name": "wc4qtz6py1i",
            "tags": [
                "40rcevshzab",
                "3ja25pufu0z"
            ],
            "quote": {
                "USD": {
                    "price": 0.2,
                },
                "BTC": {
                    "price": 7159,
                }
            }
        },
        {
            "id": 8742,
            "name": "uhso98ca",
            "tags": [
                "84jsjsaesxx",
                "sasdd5dda76"
            ],
            "quote": {
                "USD": {
                    "price": 6,
                },
                "BTC": {
                    "price": 1230,
                }
            }
        }
     ]
 }

I need to convert all of this to classes using Spring Boot. But how I should organize it? The most question is about "Data" array. For now I have something like this.

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class MainDTO{
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private Data data;
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Data {
    Coin[] coins;
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Coin{
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("name")
    private String name;
    private Map<String, Coin> quote;
}

But it doesn't work. I have error:

I have error
Tue Jan 31 16:13:45 EET 2023
There was an unexpected error (type=Internal Server Error, status=500).
Error while extracting response for type [class com.example.myCoolApp.entity.MainDTO] and content type [application/json;charset=utf-8]
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.example.myCoolApp.entity.CryptoDTO] and content type [application/json;charset=utf-8]

CodePudding user response:

You do not need a Data class because data field in your json is not an object - it is an array of objects, in your case it is an array of Coin objects.

Change it the next way:

public class MainDTO{
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private List<Coin> data;
}

CodePudding user response:

It's worth mentioning that the JSON in your example is incorrect due to extra commas. I presume the correct form should look like this:

{
    "status": {
        "timestamp": "2023-01-31T14:06:45.210Z",
        "error_code": 0,
        "error_message": null
    },
    "data": [
        {
            "id": 7982,
            "name": "wc4qtz6py1i",
            "tags": [
                "40rcevshzab",
                "3ja25pufu0z"
            ],
            "quote": {
                "USD": {
                    "price": 0.2
                },
                "BTC": {
                    "price": 7159
                }
            }
        },
        {
            "id": 8742,
            "name": "uhso98ca",
            "tags": [
                "84jsjsaesxx",
                "sasdd5dda76"
            ],
            "quote": {
                "USD": {
                    "price": 6
                },
                "BTC": {
                    "price": 1230
                }
            }
        }
     ]
 }

As for classes I would suggest you the next:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class MainDTO {
    @JsonProperty("status")
    private Status status;
    @JsonProperty("data")
    private List<Coin> coins;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Status {
    @JsonProperty("timestamp")
    private String timestamp;
    @JsonProperty("error_code")
    private Integer errorCode;
    @JsonProperty("error_message")
    private String errorMessage;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Coin {
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("name")
    private String name;
    @JsonProperty("tags")
    private List<String> tags;
    @JsonProperty("quote")
    private Map<String, Quote> quote;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Quote {
    @JsonProperty("price")
    private Double price;
}

The "data" array in the JSON response is an array of coins, so the coins property in the MainDTO class should be of type List<Coin>.

  • Related