我目前正在尝试使用这个public API :
问题是我收到超时错误,但我还没有弄清楚原因:
Generic exception: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://api.worldoftanks.eu/wot/account/info/": Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
这很奇怪,因为如果我只是输入 the request我的网络浏览器的 URL,一切都正确返回。
所以问题是:这个问题是否与对restTemplate的误解有关,或者我的请求中是否缺少与此API相关的内容。
这是我的代码:
public class PlayerWgApi {
private final String apiUri = "https://api.worldoftanks.eu/";
private final String moduleUri = "wot/";
private final String applicationId = "74613ef82f2d90e9c88a8449723936fe";
public ResponseEntity<ApiResponse> getVehicules(String accountId) {
final String methodUri = "account/info/";
final String uri = apiUri + moduleUri + methodUri;
RestTemplate restTemplate = new RestTemplate();
UriComponents builder = UriComponentsBuilder.fromHttpUrl(uri)
.queryParam("application_id", applicationId)
.queryParam("account_id", accountId)
.queryParam("extra", "statistics.random")
.queryParam("fields", "clan_id,client_language,created_at,global_rating,last_battle_time,updated_at")
.build();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<>(null, headers);
return restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, ApiResponse.class);
}
}
还有我的 ApiResponse 类:
public class ApiResponse {
String status;
Meta meta;
@JsonProperty("data")
private Map<String, PlayerResponse> player;
getters/setters...
}
最佳答案
我终于找到了为什么这不起作用,我正在使用我的智能手机数据。无论如何,它没有配置代理,所以我仍然不知道是什么参数导致了这个问题。但是当我切换到另一个网络时,一切正常。
关于java - 通用异常 :Connection timed out while using restTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59928770/