restTemplate调用返回连接超时

时间:2019-10-22 08:12:15

标签: spring-boot

我创建了一个Spring Boot项目,在其中我使用restTemplate调用rest服务。但是我得到java.net.ConnectException: Connection timed out

  • 我试图增加超时时间,但是没有用,所以我认为这不是问题。
  • 我尝试在httpclient中添加pom.xml依赖项,但再次失败。
  • 我还尝试使用postForObjectgetForEntity来代替交换功能,以便测试该项目中的其他调用,但得到的响应是相同的。
  • 也许问题与spring-boot 2.2.0版本有关,因为来自2.1.8版本的另一个项目的相同调用可以工作。但是,我想在该项目中使用较新的版本。在google搜索中,我没有发现任何版本差异可以调用restTemplate

我的pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我的RestController

import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestMe {

@GetMapping(path="/testMe", produces = MediaType.APPLICATION_JSON_VALUE)
public String testMe(){
    RestTemplate restTemplate = new RestTemplate();
    String fooResourceUrl = "https:....";

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer ****");
    HttpEntity entity = new HttpEntity(headers);

    ResponseEntity<String> response = restTemplate.exchange(fooResourceUrl, HttpMethod.GET, entity, String.class);

    return response.toString();
}
}

还有StackTrace

  

2019-10-22 10:26:28,997错误[http-nio-3268-exec-1] org.apache.juli.logging.DirectJDKLog:Servlet [dispatcherServlet]的Servlet.service()在路径为[ / ...]引发异常[请求处理失败;嵌套的异常是org.springframework.web.client.ResourceAccessException:GET请求“ https://eacrm--eadev1.my.salesforce.com/services/data/v46.0/query/”上的I / O错误:连接超时:connect;嵌套的异常是java.net.ConnectException:连接超时:具有根源的connect]       java.net.ConnectException:连接超时:connect       在java.net.DualStackPlainSocketImpl.connect0(本机方法)       在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)       在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)       在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)       在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)       在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)       在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)       在java.net.Socket.connect(Socket.java:589)       在sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666)       在sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)       在sun.net.NetworkClient.doConnect(NetworkClient.java:180)       在sun.net.www.http.HttpClient.openServer(HttpClient.java:463)       在sun.net.www.http.HttpClient.openServer(HttpClient.java:558)       在sun.net.www.protocol.https.HttpsClient。(HttpsClient.java:264)       在sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)       在sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)       在sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)       在sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)       在sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)       在sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162)       在org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)       在org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)       在org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)       在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:742)       在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)       在org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:586)       在test.api.TestMe.testMe(TestMe.java:20)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:498)       在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)       在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)       在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)       在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)       在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:634)       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:861)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1579)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)       在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:748)

预先感谢

0 个答案:

没有答案