【发布时间】:2020-01-27 09:59:29
【问题描述】:
我正在尝试使用这个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...
}
【问题讨论】:
-
代码没有问题。它对我有用。如果这不是完整的代码,请检查您是否正在设置请求或读取超时。
-
也许你在代理后面。此代理是在您的浏览器中设置的,但不是在您的代码中?
-
@Tristan,我不使用代理,我在两台不同的计算机上测试过,没有任何区别。
-
@Adi,我检查过,但我的配置中没有类似的东西。
-
@Synops 正如我所说,它对我来说工作正常,尽管响应需要几秒钟。看看这是否有帮助,stackoverflow.com/questions/5662283/…
标签: java spring api resttemplate