我正在尝试从另一个API获取响应,将其转换为Java类,然后使用Spring Boot将其发送到前端。我有来自外部API的JSON响应,如下所示:
{
"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,
}
}
}
]
}
我需要使用Spring Boot将所有内容转换为类。但是我应该如何组织它呢? 最关键的问题在于“Data”数组。 目前我有这样的一些东西。
@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;
}
但是它无法正常工作。我遇到了错误:
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]