问题描述
我今天面临的问题是这样的 - 我试图做的是从 Pokemon API 获取 Pokemon 类型的列表。响应如下所示:
{
"count": 20,"results": [
{"name": "normal","url": "https://pokeapi.co/api/v2/type/1/"},{"name": "fighting","url": "https://pokeapi.co/api/v2/type/2/"}
]
}
我实际得到的只是第一个(测试)部分,表示 count
。我留下它只是为了确保我有任何回应。但问题出在 results
上,因为我只得到空值。我不知道如何处理。
我负责申请的班级是这样的:
public class PokemonClient {
@Autowired
private RestTemplate restTemplate;
public MainResponse response() {
URI url = UriComponentsBuilder.fromHttpUrl("https://pokeapi.co/api/v2/type").build().encode().toUri();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("User-Agent","cheese");
httpentity<String> httpentity = new httpentity<>(headers);
ResponseEntity<MainResponse> response = restTemplate.exchange(url,HttpMethod.GET,httpentity,MainResponse.class);
Optional<MainResponse> search = Optional.ofNullable(response.getBody());
return search.orElseGet(MainResponse::new);
}
}
@Getter
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnkNown = true)
public class MainResponse {
@JsonProperty("count")
private Integer count;
@JsonProperty("typeList")
private GetType[] typeList;
@Override
public String toString() {
return "MainResponse{" +
"count=" + count +
",typeList=" + typeList +
'}';
}
}
@Getter
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnkNown = true)
public class GetType {
@JsonProperty("name")
private String name;
}
我确定它应该是处理带有结果的数组的东西,但是经过一天的尝试,我不知道如何处理它。请提供任何有用的指示:)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)