一般异常:使用restTemplate时连接超时

问题描述 投票:0回答:1

我目前正在尝试使用此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放到我的Web浏览器中,所有内容都会正确返回。

所以问题是:这个问题是否与对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 spring api resttemplate
1个回答
0
投票

我最终发现了为什么它不起作用,我正在使用智能手机数据。无论如何,没有配置代理,因此我仍然不知道是什么参数导致了此问题。但是,当我切换到另一个网络时,一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.