Spring RestTemplate 在 Spring 引导控制台应用程序中使用时会超时
Spring RestTemplate gives timeout when used in Spring Boot console app
我正在使用以下代码块调用外部应用程序:
String accessToken = "...";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + accessToken);
headers.set("Content-Type", "application/json");
HttpEntity<String> request = new HttpEntity<String>(UPDATE_BODY, headers);
String url = "...<url>...";
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
当我在 Spring 引导 Web 应用程序中使用代码块时,它工作正常。但是,当 运行 在 Spring 引导控制台应用程序中的同一块时,我收到以下错误:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.camunda.bpm.spring.boot.example.webapp.CallApplication.main(CallApplication.java:43) [classes/:na]
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "<...url...>": Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.camunda.bpm.spring.boot.example.webapp.delegate.CreateIdentityRecordDelegate.execute(CreateIdentityRecordDelegate.java:58) ~[classes/:na]
at org.camunda.bpm.spring.boot.example.webapp.CallApplication.run(CallApplication.java:48) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
... 5 common frames omitted
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_261]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_261]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_261]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_261]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_261]
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at sun.net.NetworkClient.doConnect(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.http.HttpClient.openServer(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.http.HttpClient.openServer(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source) ~[na:1.8.0_261]
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:739) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
... 10 common frames omitted
欢迎任何帮助或线索。
出于某种原因,需要为命令行配置代理,而无需为 Web 应用程序配置代理。不知道它从哪里得到配置。
我正在使用以下代码块调用外部应用程序:
String accessToken = "...";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + accessToken);
headers.set("Content-Type", "application/json");
HttpEntity<String> request = new HttpEntity<String>(UPDATE_BODY, headers);
String url = "...<url>...";
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
当我在 Spring 引导 Web 应用程序中使用代码块时,它工作正常。但是,当 运行 在 Spring 引导控制台应用程序中的同一块时,我收到以下错误:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.camunda.bpm.spring.boot.example.webapp.CallApplication.main(CallApplication.java:43) [classes/:na]
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "<...url...>": Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.camunda.bpm.spring.boot.example.webapp.delegate.CreateIdentityRecordDelegate.execute(CreateIdentityRecordDelegate.java:58) ~[classes/:na]
at org.camunda.bpm.spring.boot.example.webapp.CallApplication.run(CallApplication.java:48) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
... 5 common frames omitted
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_261]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_261]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_261]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_261]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_261]
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source) ~[na:1.8.0_261]
at sun.net.NetworkClient.doConnect(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.http.HttpClient.openServer(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.http.HttpClient.openServer(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) ~[na:1.8.0_261]
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source) ~[na:1.8.0_261]
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:739) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
... 10 common frames omitted
欢迎任何帮助或线索。
出于某种原因,需要为命令行配置代理,而无需为 Web 应用程序配置代理。不知道它从哪里得到配置。