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

标签 java resttemplate riot-games-api

我正在尝试访问RIOT API在 Java 中使用 Spring 的 RestTemplate 类。由于某种原因,我收到响应代码 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)情况是:

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",...

我迷路了。您知道什么可能导致此内部服务器错误以及如何修复它吗?

最佳答案

我花了一个下午终于解决了这个问题。您只需要使用这个请求 header 即可。

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

关于java - 如何使用 Java 的 RestTemplate 中的 RIOT API 获取 LOL 比赛详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72084962/

相关文章:

javascript - 为什么我的 JSON 状态在 Chrome 中返回 0,但在 IE 中却有效?

java - java fxml中图片url怎么写

java - Spring RestTemplate 将属性 HttpHeader 复制到 RequestBody

java - 从 InputStream 解压缩文件并返回另一个 InputStream

spring - 在 Spring RestTemplate 中禁用 SSL 证书验证

java - 在 Spring Boot 中使用 RestTemplate 时,Long 值始终为 null

riot-games-api - Riot API如何获取召唤师个人资料图标图像

java - 将另一个类的元素添加到数组列表

java : Understanding Arrays. asList(T...array) 原始类型方法