Java - RestTemplate - 连接被拒绝

Java - RestTemplate - Connection Refused

运行 我自己疯狂地试图解决这个问题,非常简短这是我用来执行对我的 REST 端点的调用的代码:

String url = "http://localhost:5000/getMyObject";
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(factory);

String result = restTemplate.getForObject(url, String.class);

但无论如何headers我添加它总是以连接被拒绝而告终。

Stacktrace 片段:

Caused by: java.net.ConnectException: Connection refused: connect
at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method)
...
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776)
... 74 more

设置:在 Windows 上测试 运行,在 WSL

中应用 运行

卷曲-v:

localhost:5000/getMyObject
* Trying 127.0.0.1:5000
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /getMyObject HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.68.0
> Accept: */*
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 22 Jan 2022 10:05:31 GMT
<
* Connection #0 to host localhost left intact
{<Data>}

SimpleClientHttpRequestFactory 的一些东西不能很好地播放,我将它切换到 HttpComponentsClientHttpRequestFactory,现在它工作得很好。

String url = "http://localhost:5000/getMyObject";
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory ();
RestTemplate restTemplate = new RestTemplate(factory);

String result = restTemplate.getForObject(url, String.class);