這是我從 API 得到的回應。
{"get":"statistics","parameters":{"country":"romania"},"errors":[],"results":1,"response":[{"continent":"Europe", “國家”:“羅馬尼亞”,“人口”:19016885,“病例”:{“新”:“ 4521”,“活躍”:156487,“關鍵”:431,“恢復”:2606660,“1M_pop”: "148707","total":2827936},"deaths":{"new":" 35","1M_pop":"3407","total":64789},"tests":{"1M_pop":" 1149381","總計":21857638},"天":"2022-03-24","時間":"2022-03-24T07:30:04 00:00"}]}
@RestController
public class CovidTrackerRestController {
@GetMapping("/hello")
public String showCovidInformation() {
// connect to a covid database
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://covid-193.p.rapidapi.com/statistics?country=romania"))
.header("X-RapidAPI-Host", "covid-193.p.rapidapi.com")
.header("X-RapidAPI-Key", "mykey")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = null;
try {
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// get the information
String responseString = response.body();
System.out.println(responseString);
Response romaniaData = null;
try {
romaniaData = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(responseString, Response.class);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// format the information
System.out.println(romaniaData);
// send the information to html page
return "/tracker";
}
}
And this is my Bean class which is annotated with @Bean in the configurator class alonside the RestTemplate bean. Other properties such as Cases, Deaths etc are configured same as Response class except being declared as @Bean in the configurator because from what I know once I declare a class @Bean then other references contained automatically become beans as well.
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
@JsonProperty("country")
private String country;
@JsonProperty("cases")
private Cases cases;
@JsonProperty("deaths")
private Deaths deaths;
@JsonProperty("day")
private String day;
@JsonProperty("time")
private String time;
@JsonProperty("test")
private Tests tests;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
uj5u.com熱心網友回復:
是的,你的班級應該是這樣的:
public class ResponseWrapper {
public List<Response> parameters;
public setParameters(List<Response> parameters) {
this.parameters = parameters;
}
public List<Response> getParameters() {
return parameters;
}
}
當您發布課程時,課程Response就是您的課程。您的類必須具有與 JSON 相同的結構
uj5u.com熱心網友回復:
您的 java 類需要是接收到的 json 的精確表示。讓我們稱之為Wrapper:
public class Wrapper {
@JsonProperty("response")
private List<Response> responses;
public List<Response> getResponses() {
return this.responses;
}
public void setResponses(List<Response> responses) {
this.responses = responses;
}
@Override
public String toString() {
return "Wrapper{"
"responses=" responses
'}';
}
}
我省略了一些屬性 - get,results等。看起來你不需要它們。然后反序列化將如下所示:
Wrapper data = null;
try {
data = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue("json", Wrapper.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(data);
幾點注意事項:
- 如果 json 屬性名與類中的欄位名匹配,則不需要
@JsonProperty - 對于測驗欄位注釋應該是 - @JsonProperty("tests")。屬性是
tests,不是test
如果你真的想扔掉剩下的資料,并且只需要response屬性,那么你需要撰寫自定義反序列化器并處理 json 樹。例如,您可以在我的答案或本指南中看到如何做到這一點。像這樣,您可以將回應 json 決議到您的類,即使它們的結構不匹配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448543.html
