我跟着春季啓動示例(example here)消耗一個休息電話,它調用api:http://gturnquist-quoters.cfapps.io/api/random罰款。春季引導RestTemplate調用API返回被禁止403
但後來我試着打電話給GiantBomb的api電話,我一直在禁止403錯誤。雖然我試圖與其他API調用,以及它們導致403以及(如遊戲DB API調用here)
API調用是這樣的: http://www.giantbomb.com/api/search/?api_key=[my_key]&format=json&query=「光環」 &資源=遊戲& FIELD_LIST =甲板,ID ,圖像,名稱,平臺
這是我的主要方法與春季開機配置。
@SpringBootApplication
public class ApplicationExecuter {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(ApplicationExecuter.class, args);
String url = GiantBombApiInfo.API_BASE_URL + "search/?api_key=" + GiantBombApiInfo.API_KEY + "&format=json&query=" + "\"" + "halo" + "\"" +"&resources=game"
+"&field_list=deck,id,image,name,platform";
System.out.println(url);
RestTemplate template = new RestTemplate();
ResponseEntity<Game[]> response = template.getForEntity(url , Game[].class);
Game[] games = response.getBody();
}
}
其中API_BASE_URL是http://www.giantbomb.com/api/。我打印了我正在嘗試呼叫的URL,並且使用瀏覽器訪問它的效果很好。
這個錯誤恰好發生在我調用template.getForEntity(url,Game []。class);這意味着我甚至不會去使用身體的部分。
這裏是堆棧跟蹤:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.web.client.HttpClientErrorException: 403 Forbidden
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264)
at core.exec.ApplicationExecuter.main(ApplicationExecuter.java:38)
... 6 more
Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:62)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54)
... 1 more
Caused by: org.springframework.web.client.HttpClientErrorException: 403 Forbidden
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264)
at core.exec.ApplicationExecuter.main(ApplicationExecuter.java:38)
的POM文件是波紋管:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.application</groupId>
<artifactId>appTest</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我可以爲遊戲類的代碼,將消耗JSON結果也不過就像我說的我在此之前發生錯誤。但是請讓我知道你是否需要更多的答案細節。
您能告訴我代碼中缺少什麼嗎? 謝謝
謝謝@neal!基於論壇上的討論,我不得不設置一個用戶代理。 – Salman9