如何使用 Java 的 RestTemplate 中的 RIOT API 获取 LOL 比赛详细信息?

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

我正在尝试使用 Spring 的 RestTemplate 类在 Java 中访问 RIOT API。出于某种原因,我收到一个响应代码 500,但我无法确定原因。


调用api的代码:

RestTemplate restTemplate = new RestTemplate();

String matchString = restTemplate.getForObject(
    URI.create("https://europe.api.riotgames.com/lol/match/v5/matches/EUN1_3119578792/timeline?api_key=" + Consts.API_KEY),
    String.class);

System.out.println(matchString);

例外情况是:

Exception in thread "main" org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Server Error: [{"status":{"message":"Internal server error","status_code":500}}]
    at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:100)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:172)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:714)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:333)
    at Main.main(Main.java:11)

这对我来说看起来很奇怪,因为当我使用浏览器或 Intellij Idea 的 http 客户端调用相同的 URL 时,我得到了正确的 json 响应,没有任何内部服务器错误。

{"metadata":{"dataVersion":"2","matchId":"EUN1_3119578792","participants":["9oLEhIbu5RP6HITfkJDnEy5WKOEof4Zg4TJ7GLI7ysxjzSsCpAOyyiFitA86RBpW6Tjm6l1hJHql1g","2G74UyXYGrgzOVJyhlnLvL-MBqSCOW59o1uX8KmmvDsjMeiQhrye1X6ByjmpBcIBzjNOLDZ0bExw5Q","YCqBU2zeTXQhKd9LyNn048Orj8zn2ePa2JoB7U66ql0HsVpurENBRN39HfwnmVCAr0iPX6YnvgFLFA",...

此外,对不同的 RIOT API 端点使用相同的 java 代码也可以正常工作:

RestTemplate restTemplate = new RestTemplate();

String matchString = restTemplate.getForObject(
                URI.create("https://eun1.api.riotgames.com/lol/summoner/v4/summoners/by-name/GargamelSmrdi?api_key=" + Consts.API_KEY),
                String.class);
System.out.println(matchString);

标准输出:

{"id":"_M1uK8Th4G262XkT0f9PVjUk_Dx96C4R3e7vSidKs0z5mfg","accountId":"QW10TRynWV0Zw48DrYqvO_KerzFepBCNOoUWtQHP2BFgdJZNlhtEPKXk",...

我迷路了。您是否知道可能导致此内部服务器错误的原因以及如何修复它?

java resttemplate riot-games-api
1个回答
0
投票

花了一个下午终于解决了这个问题。你只需要使用这个请求头。

HttpHeaders headers = new HttpHeaders(); headers.add("Accept","*/*");

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