通过Rest模板从外部源获取数据的java问题
我在使用RestTemplate从外部资源获取响应时遇到一些问题
我想从PokemonApi获取数据:https://pokeapi.co/api/v2/type/
我考虑创建两个包含该数据的类(不使用对象和头)
我的两门课是:
@JsonIgnoreProperties(ignoreUnknown = true)
public class MainResponse {
@JsonProperty("typeList")
private List<PokemonType> pokemonTypes;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class PokemonType {
@JsonProperty("name")
private String pokemonType;
}
我遇到的问题是无法从RestTemplate类中提取任何数据:
public MainResponse response() {
Optional<MainResponse> search = Optional.ofNullable(
restTemplate.getForObject("https://pokeapi.co/api/v2/type", MainResponse.class)
);
return search.orElseGet(MainResponse::new);
}
我不知道为什么它不能正常工作。我无法得到任何响应,当我尝试得到响应时,一切都以错误告终。请有人看看,给我一些建议
这肯定会让我在未来的RestTemplates中大开眼界:)非常感谢
共 (0) 个答案