使用 restTemplate 时通用 exception:Connection 超时
Generic exception:Connection timed out while using restTemplate
我目前正在尝试使用这个 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 class:
public class ApiResponse {
String status;
Meta meta;
@JsonProperty("data")
private Map<String, PlayerResponse> player;
getters/setters...
}
我终于找到了为什么这不起作用,我正在使用我的智能手机数据。无论如何都没有配置代理,所以我仍然不知道是什么参数导致了这个问题。但是当我切换到另一个网络时,一切正常。
我目前正在尝试使用这个 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 class:
public class ApiResponse {
String status;
Meta meta;
@JsonProperty("data")
private Map<String, PlayerResponse> player;
getters/setters...
}
我终于找到了为什么这不起作用,我正在使用我的智能手机数据。无论如何都没有配置代理,所以我仍然不知道是什么参数导致了这个问题。但是当我切换到另一个网络时,一切正常。